Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding scriptable objects in resources to dictionary

Discussion in 'Scripting' started by Corva-Nocta, Jan 16, 2022.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all! Posted a few days ago for some helped and determined the best way for me to do what I need is to get a dictionary going. So now I'm having some trouble figuring out how to actually do what I need.

    I have lots of items in a "Resources/Items" folder and I need to add then to a dictionary. They are all scriptable objects. So how do I iterate through all scriptable objects in a single folder? I'm guessing some kind of foreach loop?

    Any help at all would be great! Thanks!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Personally I don't use the Resources folder much anymore, so I'm rusty on the specifics.

    But I'd start here:
    https://docs.unity3d.com/ScriptReference/Resources.LoadAll.html

    You should be able to say something like:
    Code (csharp):
    1. var arr = Resources.LoadAll<ScriptableObject>("Items");
    And they should give you all the ScriptableObject's that are found at that path.
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    That's pretty much what I found, and it works

    Now I just have to figure out how to get an entry from a dictionary and I'll be good to go!