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

How share class between scenes WITHOUT singleton?

Discussion in 'Scripting' started by babaramanamarama, Jul 31, 2022.

  1. babaramanamarama

    babaramanamarama

    Joined:
    Oct 4, 2020
    Posts:
    34
    Hi all,
    I find singletons most ugly thing I even seen in programming. I am not going to use them ever, so question is simple - if I have a class with objects (not only basic types) and even arrays of objects, is there any way to share it between scenes WITHOUT using singletons?
     
  2. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    That's a nice summary page but amazingly they do not mention ScriptableObjects shared between scenes.



    I keep hearing people say this sort of thing and yet every time I corner someone on it, it turns out they are just repeating what they heard in programming class in high school and they actually don't have anything like an actual reason.

    Do you have a reason you feel singletons are ugly?
     
    Ryiah likes this.
  4. babaramanamarama

    babaramanamarama

    Joined:
    Oct 4, 2020
    Posts:
    34
    glgamesforfun
    Thanks, I will look at it.

    There are too many reasons:
    1. Singletons in practice are same as globals.
    2. Testing singltoned code is a hell.
    3. Singletons can become big problem for garbage collector and I find it super ugly to destroy and recreate same object, only because you can not find better solution.
    4. Singletons suck at multithreading.
    5. Singleton is controlling itself - which is absurd for OOP.
    6. Singleton uses static, which also bad idea if you need to CHANGE static members, not just use, because it is another name for GLOBAL.
    and many many more.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    So is the screen, the input device, the asset resource database, the network adapter, etc.

    For that matter, so is the player and the game... what fraction of game installations out there are actually running more than one game instance at once?

    Globals are AWESOME. One and done.

    Not in my experience. It's just code. Put a breakpoint in it or instrument it and test your code, fix your bugs.

    I don't even know what to tell you there. Clearing everything out to a blank slate and recreating everything new and afresh is one of the best ways to eliminate bugs and initialization logic flaws.

    News flash: Unity scripting is 100% single-threaded... you cannot even ATTEMPT multi-threading and manipulate the Unity API. It's completely and totally illegal, at least outside of DOTS/ECS.

    Maybe you'd be happier over in DOTS/ECS land?
     
    Ryiah likes this.
  6. babaramanamarama

    babaramanamarama

    Joined:
    Oct 4, 2020
    Posts:
    34
    Globals are another name of catastrophe - it is worst thing in programming. The only equally super terrible thing I can name is Goto command. Sorry, but I can not argue with a person, who finds globals awesome.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Yes, I suppose you cannot. That's how awesome globals are! :)

    Good luck in your travels.
     
    Ryiah likes this.
  8. babaramanamarama

    babaramanamarama

    Joined:
    Oct 4, 2020
    Posts:
    34
    Yes, that's why one WILL BE KICKED from most software companies, if he dares use globals. :) They even have mottos written on walls: "never ever dare use globals!" :)
    Good luck.
     
    Kurt-Dekker likes this.
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,135
    This. The Singleton pattern is in well known books like the "Gang of Four" design patterns book for a reason. One thing to be wary of with enterprise practices is that they're not always set for sensible reasons. There are entirely too many in that field that follow practices because that's how they've always done it and are adverse to change.

    https://en.wikipedia.org/wiki/Design_Patterns

    Worse yet they often pass their practices onto people new to the field who will then carry on the torch of choosing to use it because that's how everyone else did it even if it doesn't make sense once you step back and take a good long look at it. If you have to keep it in places everyone can see it's probably not as sensible as you thought it was.
     
    Last edited: Jul 31, 2022
    Kurt-Dekker likes this.
  10. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    The thumbnail makes this guy look like Dennis Reynolds.

    EDIT: To add something relevant to the discussion, Unity is already filled to the brim with globals and singletons. The SceneManager is static, and you can access everything through static methods in GameObject among other things.
     
    Last edited: Aug 3, 2022
    Kurt-Dekker likes this.
  11. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    507
    One alternative is to use a (static) service locator. It's still global, but solves some of the "issues" of singletones (for example unit test), so it's more accepted by the community who otherwise "hate" singletones.

    For testing (I assume you mean unit test) you can create a interface for the "service" instead of using the implementation and then use the ServiceLocator to get the interface (then you can just add a mock during unit tests)

    I usually don't need any singletones for my games, but in some rare cases global easy access is nice and I break the "rules" because most of the Unity things are static anyways, in that cases I just use my static generic ServiceLocator (less than 10 lines of code).

    Btw I work in a big company which developed software for over 25+ years (desktop application, not a game) the code base is massive but also uses a static ServiceLocator (more fancy than my private game implementation, but still static), so a "evil global" and no one was fired yet and most of the code (at least new written code) is unit tested, which works perfectly fine with the ServiceLocator.
     
    Last edited: Aug 3, 2022
    Kurt-Dekker likes this.
  12. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    People hate on singletons because they are globals, but almost all alternatives are ultimately still globals in disguise. Pretty much anything your class accesses which was not passed to it from some external code is technically a global.
     
    Kurt-Dekker likes this.