Search Unity

Discrete Unit Data

Discussion in 'Scripting' started by micahneveu, Nov 30, 2020.

  1. micahneveu

    micahneveu

    Joined:
    Jul 4, 2019
    Posts:
    7
    I'm a complete noob in Unity, but not in development, so I haven't gone through tons and tons of tutorials. However, I just need a tiny bit of help on WHERE to look to get the answer I'm after.
    Here's what I THINK I know:
    A script attached to a prefab is used on ALL prefabs that have been instantiated. So, if I have an "armor" value on that script, all prefabs using that script will share the same "armor" value.
    So, If I want EACH unit to have its own data, where do I store that? It would basically be a POCO for each instantiated unit in the game - but where do I manage that?
    I'm not looking for the answer specifically, but where/how to find it.

    Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    There are many ways to do it. You could have the script on the prefab as you described, but when you instantiate the prefab it gets deep cloned. So after it is cloned (after you call Instantiate), you can go ahead and overwrite the default values from the prefab on the returned instance and then that data will be specific to only that particular instance.
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Since you asked about where to find answers in general, the proper reply to that got to be "the documentation". And sometimes "in related tutorials / code sniplets / blogposts / ...". In this case, for example, you know that the general topic you are interrested in are prefabs. This leads us here: https://docs.unity3d.com/Manual/Prefabs.html
    There you can further read that you can either override some settings of a prefab (useful for using the same prefab with different values in different scenes), or create a prefab variant, which would be the two topics to look into.

    I believe for things like single-value adjustments or just other settings people also like using scriptable objects? But as i personally never used them i'm not too sure about any of that haha.
     
    PraetorBlue likes this.
  4. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    I do this by using ScriptableObjects. They are scene-independent data containers. You can store all unit data inside an instance of this data container definition and in your prefab, reference that instance, using its values.

    Some pseudo-code could look like this:
    Code (CSharp):
    1. public class UnitData : ScriptableObject
    2. {
    3.     [SerializeField]
    4.     private int armor;
    5.     [SerializeField]
    6.     private float health;
    7.     //...
    8.  
    9.     public int Armor => armor;
    10.     public float Health => health;
    11.     //...
    12. }
    Code (CSharp):
    1. public class Unit : ScriptableObject
    2. {
    3.     [SerializeField]
    4.     private UnitData unitData;
    5.  
    6.     [Nonserialized]
    7.     private int currentArmor;
    8.     [Nonserialized]
    9.     private float currentHealth;
    10.     //...
    11.  
    12.     public void Awake()
    13.     {
    14.         this.currentArmor = unitData.Armor;
    15.         this.currentHealth = unitData.health;
    16.         //...
    17.     }
    18. }
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Scripts on instances of prefabs don't share the same values, they just start with the same values by default. Assigning discrete values for individual prefab instances is done by editing the values in the inspector as usual; it will override the prefab's original default values.
     
    Antistone and bobisgod234 like this.
  6. micahneveu

    micahneveu

    Joined:
    Jul 4, 2019
    Posts:
    7
    Thank you all for the information and help. This was plenty enough to get me closer to my answer.