- Home /
This question was
closed Feb 13, 2020 at 08:27 PM by
ZachRoman121 for the following reason:
Other
Question by
ZachRoman121 · Feb 14, 2017 at 01:45 AM ·
scriptableobjectresources
Resources.LoadAllOfType With ScriptableObjects?
I am trying to add every ScriptableObject i find to a list. The ScriptableObject is called "Items"
I already declared itemsList
Here's my code:
public void loadAllItemsInList() {
itemsList.Clear();
foreach (Items item in Resources.FindObjectsOfTypeAll(typeof(Items)) as Items[]) {
itemsList.Add(item);
Debug.Log (item.itemName);
}
}
It seems like it should work. But it doesn't. It just returns an error showing me where i declared my Foreach. And. Saying "Object reference not set to an instance of an object". No clue how to fix this
Comment
Best Answer
Answer by jdean300 · Feb 14, 2017 at 01:55 AM
You cannot cast an Object[]
to an Items[]
. What you can do is use LINQ to convert the Objects into Items:
foreach (Items item in Resources.FindObjectsOfTypeAll(typeof(Items)).Cast<Items>())