Search Unity

I only have Instance.this

Discussion in 'Scripting' started by drndfx, Nov 1, 2020.

  1. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    I'm referencing variables between different codes using instances such as Player.instance.health;.
    What's the difference between only having

    Code (CSharp):
    1. private void Awake()
    2.     {
    3.        instance = this;
    4.     }
    as opposed to


    Code (CSharp):
    1. private void Awake()
    2.     {
    3.         if (Instance == null)
    4.         {
    5.             Instance = this;
    6.     DontDestroyOnLoad(gameObject);
    7.         }
    8. else
    9.     {
    10.     Destroy(gameObject);
    11. }
    12.     }
     
    Last edited: Nov 2, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    The first will overwrite the value of instance every time Awake is run. This is useful if you want to create a static reference to a script but you don't plan to have the script float. By definition, this is not a singleton.

    The second is a singleton pattern. It checks if Instance has a value, and if not, it assigns the script to it and sets the gameobject to not be destroyed (thus is floats between scenes). If it has a value, then it destroys the new copy. You have to do this to avoid having multiple copies of a script that you only want one of. Like a GameManager script or AccountManager.
     
    Vryken likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
     
    Joe-Censored likes this.
  4. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Thank you. Now I understand how the Singleton works.

    So, just to reference code Player.instance.health; between two scripts this should be sufficient, right?

    Code (CSharp):
    1. private void Awake()
    2.     {
    3.        instance = this;
    4.     }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Yes indeed, as long as you keep in mind order of initialization. All Awakes will be called before all Starts, assuming all objects are in scene and enabled.

    Beware that you may hear chuff from computer-science-y people about how the above is an anti-pattern. Fortunately such patterns exist to help us who actually make and ship games deliver them in a timely fashion. :)

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Bunny83 and Joe-Censored like this.
  6. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Thank you all for the answers.