@peterdrake Except then static variables persist between sessions, which can easily break poorly-implemented singleton patterns. Be *very* careful.

If your builds are taking a long time, I highly recommend letting unity refresh the Library folder (just close Unity, delete the library folder from your project, and re-open unity). It'll take Unity a while to rebuild that folder, but then script builds should be *much* faster for a while.

@LouisIngenthron Can you say more about properly implementing singletons? A web search reveals a deep rabbit hole on this, but all of them use static variables.

@peterdrake Oh, you still have to use statics, you just have to be smart about it. This is the pattern I use that hasn't failed yet. Put it in the Start() function of MonoBehaviours. Try to avoid singletons of non-unity-managed-objects (you'd need a different pattern than the below).

if(Singleton == null)
{
Singleton = this;
DontDestroyOnLoad(gameObject);
}
else if(Singleton != this)
{
Destroy(gameObject); // two in scene
}

As long as you do that for Unity objects, that first null check will use Unity's override == check to also check for destroyed or unreferenceable objects.

And then in the Destroy function:

if(Singleton != null && Singleton == this)
{
Singleton = null;
}

Follow

@peterdrake Basically, the key is to make sure to clean up after yourself since the editor won't do it for you. So, if you have a static score integer, make sure somebody's start function resets it to zero. Stuff like that.

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.