Search Unity

Using a prefab or ScriptableObject without instantiating or creating?

Discussion in 'Editor & General Support' started by akutruff, Aug 12, 2015.

  1. akutruff

    akutruff

    Joined:
    Jul 24, 2009
    Posts:
    44
    Hi all,

    I have a prefab that essentially is just a lookup table for other prefabs:

    OrbPrefabTable
    -List<int> Ids
    -List<GameObject> Prefabs


    I have another script on an object in my scene that has a direct reference to the OrbPrefabTable script on a prefab in my project. I simply access the values on the prefab asset directly from my code without first instantiating the prefab. I do not alter the values on the prefab or use it any other way. It is treated as entirely immutable. Is there a problem with this? The Editor is crashing on occasion and sometimes the prefab gets another Transform added to it when I exit play mode.

    The same goes for ScriptableObjects - do I actually have to create an instance of them before I use them?

    I have a feeling this is a case where the Unity Serialization system is broken.
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You should ditch your prefab and use a ScriptableObject instead. You don't need to instantiate these.
     
    zombiegorilla likes this.
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    What @blizzy said. You don't access objects in the project from the scene like that. You need to instantiate it, or use a ScriptableObject.
     
  4. akutruff

    akutruff

    Joined:
    Jul 24, 2009
    Posts:
    44
    I can see why I "shouldn't" but unity lets you without problem, and I have been doing it this way for years. I only started questioning the technique recently due to errors I have been getting in combination with community chatter concerning serialization problems.

    Switching to ScripatableObjects is worth a try. They have so much overhead in so far as editor scripting and making inspectors though, which is a bummer.
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    They overhead is minimal. You can make them more complex if you want, but typically I don't bother creating custom inspectors for them. Once created, the public vars are all available in the inspector like any other.

    I am surprised that accessing values on a prefab in the project would work. I wouldn't have expected it to, that is kind of what ScriptableObjects are designed for.
     
    blizzy likes this.
  6. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    You can also use VFW to ease the pain with custom inspectors a little.

    Same, and I would strongly recommend against doing it this way.
     
    zombiegorilla likes this.
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    I'm a pretty big fan of drawers, I use them for tons of things. ;)
     
  8. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I rarely do custom stuff, but rather rely on the default drawers it provides. But I feel VFW can serialize more specialized constructs than Unity can.
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    You don't instantiate these, but you should load them before using them.

    What overhead are you referring to? There's only the minor issue that you need to create the ScriptableObject asset and save it into your project. This helper script (ScriptableObject Factory) can solve that issue for you.

    You don't have to create a custom inspector for it, Unity will use the default one so you can edit it with no issues.