Search Unity

ScriptableObject and Resources.FindObjectsOfTypeAll

Discussion in 'Getting Started' started by Stradmann, Jan 20, 2019.

  1. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    31
    Hi!, Now,I'm working a litle bit with Scriptable Objects, and i use to use it to store data. For example, i created a Character ScriptableObject to store characters and it's characteristics; or to manage some data about the playgame from scene to scene. In the first case, the problem is that i have lot of "Character" asset, lots of "Mision" asset, and what i want is to search them and put them into an array. For this purpose i tried to use Resources.FindObjectsOfTypeAll. It seems that works well, but after some test of the game, or if i change the scene in the editor to test, it doesn't work anymore. Changing all that scriptableobjects to other folder, ever under Resources folder, it makes work again fr a couple of times or until i load other scene in the editor.
    What's happening? and How i can make that it works ever? There is other way to collect all that SO into an array automatically?
     
  2. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    31
    Any help about how i have to use appropriately Rseources.FindObjectsOfTypeAll ??
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    397
    From the docs of Resources.FindObjectsOfTypeAll:
    "This function can return any type of Unity object that is loaded, […]"

    Wether the scriptable objects are loaded at any given time in the editor is pretty much random and you shouldn't rely on it. The scriptable objects will likely also not be included in builds, FindObjectsOfTypeAll should therefore not be used to load assets.

    You can put your scriptable objects into a "Resources" folder and then use Resources.Load. The Resources folder will make sure they are included in builds and the Load methods make sure they will be loaded.

    However, it's more idiomatic in Unity to use explicit references, i.e. create a script that references all your scriptable objects. This can be more cumbersome to set up but then Unity will be able to figure out which of your scriptable objects are actually in use and will load them transparently.
     
    Kiwasi likes this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    2,889
    You could also make a single ScriptableObject that contains an array of a custom class for your character data. Then you'd just assign the one object where it's needed to get access to all your character definitions.
     
    Ryiah and Kiwasi like this.
  5. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    31
    thanks a lot!! Now i'm going to study your answers.
     
  6. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    31
    i tried Resorces.Load and seems that it makes an array of objects...so can i use it from here avoiding use Resorces.FindObjectsOfTypeAll?
    Now that i've organized all my SO in folders under Resources folder, i know that all my objects i load with a path are the same type of object. The path is sometehibg like Resources/ScriptableObjects/Misions... so if i use Resources.LoadAll("ScriptableObjects") it will load all in this folder and subfolders, is'n it?
    Having all that objects in array, how i search for a list or array of a specific type? Something like to say: Get all mision type objects of this array and put them into a list, or other array
     
  7. Stradmann

    Stradmann

    Joined:
    Jun 11, 2018
    Posts:
    31
    Ahh! fooreach loop of course... Thanks a lot for all!!
     
  8. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    26
    How do you reference a scriptable object just to ensure it is loaded so later can be found by FindObjectsOfTypeAll? Please is there a code example?

    If you only need a reference of this scriptableobject is it a good idea to use a singleton with a reference of this singleton returned by FindObjectsOfTypeAll?

    Thanks!
     
  9. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    397
    @Jorhoto
    Generally, don't use FindObjectsOfTypeAll at all. It's a kludge and you shouldn't use it to load assets. As you have noticed, assets might not be loaded or might not be included in builds when you use FindObjectsOfTypeAll.

    In Unity, everything starts with your scenes. To properly reference a scriptable object, it needs to be referenced by a script in your scene. Then you want to access your scriptable object using that script instance and Unity will make sure it's loaded and included in builds.

    You probably want to have some "global" game objects in your scenes (you can use prefabs, MonoBehaviour singletons, additive loading of a global scene, DontDestroyOnLoad() etc.) where you can put scripts with references that are shared by many parts of your game, so that you don't need to set the same references over and over.
     
    Jorhoto likes this.
unityunity