Can someone tell me about this static instance

I learned how to create what I only know as a static instance when it was briefly mentioned in a tutorial I watched a while ago.

I’ve been implementing this functionality in my scripts. But I don’t know exactly what it’s referred to as from a software engineering perspective.

Short example:

public class GameplayManager : MonoBehaviour
{
    public static GameplayManager Instance { get; set; }

    void Start()
    {
        Instance = this;
    }

    public void Play()
    {

    }
}

This code allows me to access a public function from this script from anywhere by writing:

GameplayManager.Instance.Play();

That’s great, but what are the implications of this? And what is this format referred to as? I know this script should only be used once in my scene as it’s a static. But I rarely see this sort of code in other scripts so I’d like to make sure I’m on the right track.

Thanks.

It’s called a singleton. It’s a controversial pattern. Used properly it’s powerful. Abused it can destroy projects.

There is actually a long thread in the general dissusion forum at the moment on the topic.

1 Like