C
C#•2y ago
Kevin_

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
Buddy
Buddy•2y ago
Singleton is ok in some instances, overusing them is a bad idea. But you can use them from time to time.
Kevin_
Kevin_OP•2y ago
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?
Buddy
Buddy•2y ago
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?
Kevin_
Kevin_OP•2y ago
More of like maxHealth, currentHealth, bulletDamage etc
Kevin_
Kevin_OP•2y ago
Buddy
Buddy•2y ago
Then no, do not use a singleton. As it's related to that specific player, not all of them.
Kevin_
Kevin_OP•2y ago
I don't intend on making it multiplayer or have other players, just a single character.
Buddy
Buddy•2y ago
It's still a good idea to not use a singleton Not really sure why you need a singleton for that? 🤔
Kevin_
Kevin_OP•2y ago
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
Buddy
Buddy•2y ago
Its better to leave room open if someone requests cough co-op, or even you want it in. Plans can change.
Kevin_
Kevin_OP•2y ago
Gotcha, so you would lose that flexibility room if you choose to implement it
Buddy
Buddy•2y ago
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.
Kevin_
Kevin_OP•2y ago
Gotcha! Its better to have more flexibility rather than in this case take the easy way out.
Buddy
Buddy•2y ago
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.
Kevin_
Kevin_OP•2y ago
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.
Buddy
Buddy•2y ago
https://gameprogrammingpatterns.com/object-pool.html If you are intrested in object pooling.
Kevin_
Kevin_OP•2y ago
Thanks I'll bookmark the link!
Buddy
Buddy•2y ago
You can keep a reference of player in the bullet, it's completely fine.
Kevin_
Kevin_OP•2y ago
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
[SerializeField] private PlayerStats _playerStats;

private EnemyStat _enemyStat;

private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Enemy"))
{
_enemyStat = col.gameObject.GetComponent<EnemyStat>();

_enemyStat.health -= _playerStats.bulletDamage; // Subtracts enemys health by bulletDamage

Destroy(gameObject); // Removes bullet from world
}
[SerializeField] private PlayerStats _playerStats;

private EnemyStat _enemyStat;

private void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.CompareTag("Enemy"))
{
_enemyStat = col.gameObject.GetComponent<EnemyStat>();

_enemyStat.health -= _playerStats.bulletDamage; // Subtracts enemys health by bulletDamage

Destroy(gameObject); // Removes bullet from world
}
Buddy
Buddy•2y ago
Keep a reference to the player and not player stats
Kevin_
Kevin_OP•2y ago
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?
Want results from more Discord servers?
Add your server