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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Singleton instances removing serilizefiled data

Discussion in 'Scripting' started by LonerD11bg, May 7, 2022.

  1. LonerD11bg

    LonerD11bg

    Joined:
    May 6, 2022
    Posts:
    3
    First I would like to address that I am still very new to unity and have a limited knowledge of what I am actually doing so this may be a bit confusing to anyone who has more knowledge than I do.

    So my problem is that I have 3 singletons in my game. They are the scoreKeeper to keep score across all levels, a player, and a levelManager because when it was not a singleton it would throw errors about being missing.

    Now the problem I am having is that I have serializefields that require the player and the levelmanager. This works fine until I change the level and then the carried over singleton will not populate the serializefields they need to be in.

    How do I fix this?!

    As of now my health bar will not work once I get through the first level and once I die and try to restart the restart button on the game over menu will not work but the main menu button will. Clicking the main menu button will work but the start game but on the main menu no longer does.

    LevelManagerProblem.PNG Playerproblem.PNG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Two ways:

    - have singleton lifetimes equal to the items they refer to. One way is to parent things the singleton refers to under the singleton, or vice versa

    - have singleton references able to be restored when they get invalidated (perhaps by finding afresh)

    Personally, I will NEVER stick singleton-ish things into a scene. Instead I always use on demand loading an lazy resource finding and loading. Here's how:

    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.
     
  3. LonerD11bg

    LonerD11bg

    Joined:
    May 6, 2022
    Posts:
    3
    Ok so I realized that I had no need for the player to be a singleton since nothing is being carried over to other scenes and that fixed the health bar issue however I'm still a bit confused on how your solution will fix my problem. Like I said I am still pretty new to Unity and all of this so could you dumb it down for me please.

    Also I don't know if this matters but I currently only have my levelmanager actually put in to my mainmenu scene all my other scenes use the singleton instance that is created at game launch.
     
  4. LonerD11bg

    LonerD11bg

    Joined:
    May 6, 2022
    Posts:
    3
    Kurt-Dekker,
    Thank you very much for your help! After studying my code more I discovered that I was turning things into singletons that didn't need to be. So I apologize for wasting your time but I do appreciate you taking the time to try to help me out.
     
    Kurt-Dekker likes this.