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

Scriptable Object Singletons, why use them?

Discussion in 'Scripting' started by stephenq80, May 26, 2021.

  1. stephenq80

    stephenq80

    Joined:
    Jun 20, 2018
    Posts:
    34
    Hi,

    I recently started looking up information on game architecture and I've heard that using Singletons is not a good idea. I have also started to learn more about scriptable objects and how they are just amazing. I recently watched the Unity Open Projects video called Chop Chop and it went on and on about how good scriptable objects. And around the same time, I learned about Scriptable Object Singletons and they have me confused.

    What I can't fully wrap my head around is that, if singletons are bad, why is a scriptable object singleton better?

    Also, I was watching the Unity Learn Swords and Shovels courses and I noticed something. During the character stats video, the instructor created a scriptable object that contained all of the stats and methods to manipulate them.

    My question regarding that is, is something like that a good idea because I thought that SO's should be used like a data container that you pull from not write to? If it is okay to do it with the character's stats, which I assume will change as he levels up, why not do it with the inventory and equipment also. Just make a Inventory SO, and plug all the logic in like the stats and just inject it into the mono behavior that needs it? What makes one use case better than the other?

    Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    They're not.

    Maybe you should ask that question of whoever is making the claim that it is, because I have never heard of a scriptable object singleton.

    The main argument I've heard against singletons is basically that they're too good - that is to say, they're so convenient and useful that you might get in the habit of using them everywhere, and they're not appropriate everywhere. So as long as you limit singletons to where they're appropriate - that is, limit them to a situation where you will always only have one copy of something - they're good. And they're particularly good in Unity, because there's no easy way to edit fields and assign references in a plain static class.

    I'm not even sure offhand how one would even make a ScriptableObject Singleton, at least a useful one. They don't have methods like Awake() that you can use to self-assign the static reference, so you'd have to assign the reference through some other script that either has a reference or calls something like Resources.Load... and at that point you really just have a manager, not a singleton. You could then shoehorn a singleton into that, but why would you?
     
    Last edited: May 26, 2021
    Lurking-Ninja likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Novack, RaL, eterlan and 1 other person like this.
  4. stephenq80

    stephenq80

    Joined:
    Jun 20, 2018
    Posts:
    34
    Trust me, you're not alone.

    Watch:
     
    Whitebrim likes this.
  5. stephenq80

    stephenq80

    Joined:
    Jun 20, 2018
    Posts:
    34
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    I'd like to note that the video you linked is talking about how to implement an equivalent to a class that, apparently, already exists in recent versions of Unity. Unity's version even has a class attribute that presumably creates and manages the file for you.

    So with that busy-work done for us, suddenly a ScriptableSingleton is actually wildly useful! For all the same reasons that singletons are useful, but it makes it much harder to run afoul of the situations where singletons are not appropriate as described in my first comment.
     
    Atomiz2002 likes this.
  7. stephenq80

    stephenq80

    Joined:
    Jun 20, 2018
    Posts:
    34
    Thanks for that. So, with that being said, these are actually better than the plain o'le singleton? It would be safe (even recommended) to refactor, say for example, my inventory class singleton to a scriptable object singleton?
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    Just to cool your enthusiasm...

    ScriptableSingletons are editor only, part of the
    UnityEditor
    namespace. :)
     
    Dalsia, Novack and punkrooks like this.
  9. mishakozlov74

    mishakozlov74

    Joined:
    Aug 8, 2018
    Posts:
    133
    Regarding Singleton as a pattern, there are two types of people, who don't like it and *you can't call private constructor*

    So, mostly people argue about the thing that it restricts you from creating another instances. A lot of good programmers say that it's okay to mean class to have one instance, but not to make it impossible to create another one.

    Singleton is very-very popular and tricky pattern. I personally using only when I know that creating another instance of the class will break something.

    I think you have already ran across Game Development Patterns which has quite nice description about it. I'd trust it.
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Suddenly a ScriptableSingleton is actually wildly worthless!

    WHYYYY would they make a base class so useful and then just.... URRRRGH.
     
  11. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    If I had to guess it's because of the persistence. But yeah, I wish it was runtime too.
     
  12. punkrooks

    punkrooks

    Joined:
    Apr 15, 2016
    Posts:
    34
  13. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    Ooh, that's very useful for editor tools though!
     
    logicandchaos likes this.
  14. stevenmare

    stevenmare

    Joined:
    Jul 20, 2018
    Posts:
    76
  15. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Personally speaking, whilst the idea of adddressables seems good and solves the issues mentioned in that article; In practise, the async only method of loading has always caused me too many headaches to make them practical to use.

    I'll be sticking with Resources for the time being, as long as they aren't abused, there is no problem with them.
     
  16. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    You can load addressables synchronously if that suits your needs better: https://docs.unity3d.com/Packages/com.unity.addressables@1.17/manual/SynchronousAddressables.html

    Personally I've had zero issues using async code to work with addressables.
     
  17. toddwong

    toddwong

    Joined:
    Aug 29, 2017
    Posts:
    10
    Singleton is "evil" because it is too easy to be abused. I have deal with a project with a big singleton global state object, and most of the time, no one knows who and when and where changed that state, because anycode can change it at anytime at anywhere. Avoid any stateful singleton as possible as you!
     
    Thaina, logicandchaos and Northmaen like this.