Search Unity

Singleton does not work as expected

Discussion in 'Scripting' started by HP_tesnn, Jun 28, 2022.

  1. HP_tesnn

    HP_tesnn

    Joined:
    Oct 17, 2020
    Posts:
    30
    I have 2 classes UIController and GameController inherit a generic Singleton class.
    Code (csharp):
    1.  
    2. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
    3. {
    4.     static T ins;
    5.     public static T Ins { get { return ins; } private set { ins = value; } }
    6.     void Awake()
    7.     {
    8.         if (ins != null && ins != this)
    9.         {
    10.             Destroy(gameObject);
    11.         }
    12.         else
    13.         {
    14.             ins = this as T;
    15.             DontDestroyOnLoad(gameObject);
    16.         }
    17.     }
    18. }
    19.  
    But when I start the game, only GameController is created in the Don'tDestroyOnLoad field and the UIController is not. Where did I do wrong?
     
    Last edited: Jun 29, 2022
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Here you go:


    Code (CSharp):
    1.     public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
    2.     {
    3.         private static T instance = null;
    4.         private static readonly Object syncRoot = new Object();
    5.  
    6.         public static T Instance
    7.         {
    8.             get
    9.             {
    10.                 if (instance == null)
    11.                 {
    12.                     lock (syncRoot)
    13.                     {
    14.                         if (instance == null)
    15.                         {
    16.                             instance = FindObjectOfType(typeof(T)) as T;
    17.                             if (instance == null)
    18.                                 Debug.LogError(
    19.                                     "SingletoneBase<T>: Could not found GameObject of type " + typeof(T).Name);
    20.                         }
    21.                     }
    22.                 }
    23.  
    24.                 return instance;
    25.             }
    26.         }
    27.     }
     
    HP_tesnn and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Good stuff @Terraya... and here's my personal favorite. :)

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

    And if you really insist on barebones C# singleton, here's a Highlander:

    https://gist.github.com/kurtdekker/b860fe6734583f8dc70eec475b1e7163

    Also, for OP, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.
     
    Terraya and HP_tesnn like this.
  4. HP_tesnn

    HP_tesnn

    Joined:
    Oct 17, 2020
    Posts:
    30
    Thank you so much. Both answers are great answers for me.
     
    Kurt-Dekker likes this.