Home > other >  How do I make an array with all Scriptable Objects from a folder in Unity?
How do I make an array with all Scriptable Objects from a folder in Unity?

Time:06-26

So this is the Scriptable Object:

[CreateAssetMenu(fileName = "New Char", menuName = "Char")]
public class ScriptableChar : ScriptableObject
{
public string charName;

public Sprite charSprite;
public Sprite charBorderSprite;

public CharClass charClass;
}

And I have tried this to get the array:

public Object[] allCharacters = Resources.LoadAll("Cards");

and this:

void GetAllCharactersArray()
{
ScriptableChar[] allCharacters = Resources.LoadAll<ScriptableChar>("Cards");
Debug.Log("Characters: " allCharacters.Length);
}

I have also tried a few other things I have deleted because they haven't worked either.

Image of the Folder structure if that is needed or matters in any way.

I would be really happy if someone can help, I have been trying to solve this for many hours now.

CodePudding user response:

Okay, so first things first: Resources only works for assets in a folder called "Resources". You can't load the assets from "Assets/Cards", because they are not located in a Resources folder and not indexed to be loaded by the helper class. See the docs for Resources.


Resources are bad practice. I don't know what you want to do, but I assume, you want to access the cards and other assets somewhere. A common way to do would be to create a ScriptableObject that references all of these assets in lists and then use a reference to an instance of this object anywhere you want to access the data.

If you want to auto-assign the references in the editor, you can do so using an Editor script that searches in the folders, picks up all the references and stores them in the instance of said ScriptableObject.

  • Related