Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

globally available instantiated class? Please help a c# noob

Discussion in 'Scripting' started by rpuls, Aug 17, 2022.

  1. rpuls

    rpuls

    Joined:
    Feb 3, 2017
    Posts:
    101
    Hello, I'm running into a problem that I have only been able to partly solve.

    I have a SavedGameManager class (singleton) which has SaveToCloud and LoadFromCloud functions. I need to call these functions from multiple scenes in my game.

    First I instantiated the class by attaching it to a GameObject in my main menu scene. So when the game initially starts up I could load the players game data. But in order to also be able to call the save and load functions from within other scenes I added DontDestroy to the class so It wouldn't disappear when I switch between scenes.

    This is all good for a compiled game that ensures starting from the main menu, but a really annoying side effect of this approach is that I cannot start any other scene directly in the unity editor when developing... the GameObject will not exist and when calling SavedGameManager.instance.Save() I'm getting the error: NullReferenceException: Object reference not set to an instance of an object.

    Are there any way to insatiate the class and make it globally available to all scenes some how?
     
  2. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    put an empty game object with your SavedGameManager script and put it on every scene. test this and share you script, maybe you did not add make the method shared on all classes using the static keyword.
     
  3. gamedev1506

    gamedev1506

    Joined:
    Nov 2, 2019
    Posts:
    36
    If your "SaveToCloud and LoadFromCloud" functions dont need data, they could be static and do their job even without a singleton instance in the scene.
    Also you could use a ScriptableObject, this way all objects that depend on this "singleton" data could directly reference it in any scene.
    Or use multiple additive scenes, keep a global scene with global singletons loaded all the time, in the editor you would still need to reopen 2 scenes to use it (global and the scene you are working on).
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    I always use one of these approaches:

    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
     
    Bunny83 likes this.
  5. rpuls

    rpuls

    Joined:
    Feb 3, 2017
    Posts:
    101
    I would like to avoid having to place a GameObject just for that in ever scene (100+ scenes...) I's like to have a autoscaling and less error prone solution for this. ;)


    They do ;) they are chaching a lot of data



    Yay ! awesome man :) this is exactly what I need sweet - thanks a lot !
     
    TheDevloper and Kurt-Dekker like this.