Unity Script Communication
Hi, I was wondering when is a good use case to implement a Singletons and when are they not? I'm currently debating on using one but I've heard really missed opinions on the use of singletons
21 Replies
Singleton is ok in some instances, overusing them is a bad idea. But you can use them from time to time.
Gotcha! Would making a singleton for my players stats be a optimal way of doing it or should I find another method of getting those stats?
Not good to use singleton for that
Keep them referenced on the player component, and do not use a singleton
Oh, are you talking about total damage dealt etc?
Those stats?
More of like maxHealth, currentHealth, bulletDamage etc
Then no, do not use a singleton.
As it's related to that specific player, not all of them.
I don't intend on making it multiplayer or have other players, just a single character.
It's still a good idea to not use a singleton
Not really sure why you need a singleton for that? 🤔
Okay, sounds good! Is there a specific reason on why I shouldn't so I can avoid it in the future?
I was mainly using it so I can easily access those variables rather than constantly getting the script associated with them
Its better to leave room open if someone requests cough co-op, or even you want it in. Plans can change.
Gotcha, so you would lose that flexibility room if you choose to implement it
You should consider this:
- Is it maintainable?
- How well would it work?
You'd completely break things if you decided, nope. I want this to be a 2-player game.
Which would mean, no. It isn't really maintainable as it would take a lot of effort for something that could've been avoided to begin with.
Gotcha! Its better to have more flexibility rather than in this case take the easy way out.
If you want to optimize.
For example if you're making a bullet-hell. You'd need an object pool.
That way you don't have to call instantiate / destroy for each projectile.
Just instantiate ~1000 disabled objects in the start, keep them in a collection.
pop the list when you need an object, and push it back in when the object is not being used anymore.
Interesting. I am making a bullet-hell game so I'll keep that noted
I've been working on stats system which I completed today, but I've been running into an issue where if I pick up a
bulletDamage Buff
its updates the stats but still only does the original damage. Thus I thought about using a singleton from what I understand its not good to use it. I don't understand if the reference is null or something else causing the issue.https://gameprogrammingpatterns.com/object-pool.html
If you are intrested in object pooling.
Thanks I'll bookmark the link!
You can keep a reference of player in the bullet, it's completely fine.
What I'm currently doing is this, but for some reason in this script only bulletDamage does not update according to the stats the player picks up
Keep a reference to the player and not player stats
How come?
I found out that this code is referencing another version of the player stats for some reason rather than getting the updated one
Would you happen to know why a script would be referencing another version of the player stats event tho it's a SerializedField?