brunomikoski/ScriptableObjectCollection

Using .GetOrAddNew<T> throws a InvalidOperationException if the item does not exist

Thundernerd opened this issue · 0 comments

Like the title says, calling .GetOrAddNew<T> throws an InvalidOperationException if the item does not already exist.
The reason is that it isn't calling FirstOrDefault() but it is calling First().

File: ScriptableObjectCollection.cs
Line: 427-434

public ObjectType GetOrAddNew(string targetName)
{
    ObjectType item = Items.First(o => o.name.Equals(targetName, StringComparison.Ordinal)) as ObjectType;
    if (item != null)
        return item;

    return AddNew(targetName);
}

Workaround:

Use .GetOrAddNew(typeof(...), "...");