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

Question Best practice for using static with prefabs

Discussion in 'Scripting' started by Chusuk, Dec 16, 2022.

  1. Chusuk

    Chusuk

    Joined:
    Apr 19, 2021
    Posts:
    43
    Any help much appreciated on the following scenario...

    Say I want to create a million trees, randomly from a selection of 10 tree prefabs.

    These trees share a set of common traits and for simplicity, lets say its these:
    • Max Health
    • Player level required to do damage
    • type of fruit to spawn
    • List of local Vector3 of where the fruit spawns
    I could create a script with a class that contains this information as variables and attach it to each of the tree prefabs. Then if I were to RayCast and hit a tree, I could get the common script component and look at variable I need in order to make decisions in code.

    I'd like to make this data static so there aren't a million instances of data created with all of the trees, when in reality there are only 10 instances required (one for each tree type).

    Can I create some kind of reference in the prefab to a class that contains static variables for these traits...but make the common class static on a 'per tree type' basis?

    I guess my problem is I can't think of an easy way to have a common component structure and name (so my RayCast processing code is efficient) but have that component be static per tree type to prevent unnecessary duplication of data.

    I could code an array of tree data and store a "tree type" cross reference variable within the prefab to look up the static data required, but it would be nice to be able to use the inspector to keep the data with the pertinent prefab so as to reduce errors and made workflow more logical.

    It's probably a simple thing I'm missing, I just don't have the Unity/C# experience to see it. The old programmer in me just cringes at all the duplication of data.

    Thoughts appreciated...
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Put the immutable data on a scriptable object. Reference the scriptable object on your prefab. Easy done.
     
    Last edited: Dec 16, 2022
    Chusuk likes this.
  3. Chusuk

    Chusuk

    Joined:
    Apr 19, 2021
    Posts:
    43
    Ha! See, I knew it was a simple thing I was missing!

    First three sentences of the description in the docs sums up my post nicely :)

    "A ScriptableObject is a data container that you can use to save large amounts of data, independent of class instances. One of the main use cases for ScriptableObjects is to reduce your Project’s memory usage by avoiding copies of values. This is useful if your Project has a Prefab that stores unchanging data in attached MonoBehaviour scripts"

    Thanks spiney, much appreciated.
     
    spiney199 likes this.