Search Unity

How do you do an instance singleton with the newest version of unity?

Discussion in 'Scripting' started by Ziron999, May 24, 2019.

  1. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    Code (CSharp):
    1.  
    2. public class BC_Singleton : MonoBehaviour
    3. {
    4.     private static BC_Singleton instance = null;
    5.     public static BC_Singleton Instance
    6.     {
    7.         get
    8.         {
    9.             if (instance == null)
    10.             {
    11.                 instance = (BC_Singleton)FindObjectOfType(typeof(BC_Singleton));
    12.             }
    13.             return instance;
    14.         }
    15.     }
    16. }
    was the best thing ever since sliced bread and now we can't do it...what's the new way to do this?
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Why? What's wrong?
     
    Vryken, Dextozz and xVergilx like this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your method should work just fine.
    Here's another way you could go about it:
    Code (CSharp):
    1. public class Foo : MonoBehavior {
    2.    public static Foo instance = null;
    3.  
    4.    private void Awake() {
    5.       if(instance == null) {
    6.          instance = this;
    7.       }
    8.       else if (instance != this) {
    9.          Destroy(this);
    10.       }
    11.    }
    12. }
    If it's still not working, then something else is the issue. Have you actually added an instance of your object to the scene?
     
    Last edited: May 25, 2019
  4. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    unity says an error in the latest version saying FindObjectOfType( is only allowed in start and awake now :(
     
  5. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    or start with a boot scene, a scene which only handles initialising at start up of your game but never gets loaded again after game start up.

    boot --> menu --> game --> game over --> menu --> game and so on

    in boot scene you create an empty gameObject with script attached with DontDestroyOnLoad(gameObject) in the Awake method.
     
  6. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    165
    That's not true. Are you sure your class is MonoBehaviour at all?
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Post the exact error message. The closest I can think of is that perhaps it says something like "can only be called from the main thread".

    Also, you'll be better off using the generic version of the method, i.e. 'FindObjectOfType<YouTypeHere>()'.
     
  8. Ziron999

    Ziron999

    Joined:
    Jan 22, 2014
    Posts:
    282
    well i already fixed it by just putting it in awake so it is no longer an issue. this did make me change a lot of code i didn't realize was initializing in an order before this one though but...it is what it is..
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you had just put the exact error message, you could've saved the time for the changes. If you're still interested in a solution (or at least an explanation why those errors occured), you can still post it. Perhaps it's just a small detail that you've missed.