Static Variable

Hello, I’m watching the “Learn to Code by Making Games” Course for unity atm. I understand what Static variables do but the way the person in the video is using them is confusing me a little and I was hoping to get a little bit of clarification. I always thought the only purpose of a static variable is to be able to access that variable in another class by simply adding the class name and the name of the static variable. However this person is using a static variable and we aren’t going to be having to call it in a different class, or I guess in unity “Script”.
Let me explain what we are doing and then I’ll paste code for visual looks and hopefully you guys can help me understand. What we are trying to do is make it so that music will continue to play even while we switch through levels and when we come back to the start screen the music doesn’t duplicate and start playing 2 tracks at once which we have done. But apparently a static variable is needed for this and I don’t get why.
Code:

static MusicPlayer instance = null;


    // Use this for initialization
    void Start () {
        if (instance != null) {
            Destroy(gameObject);
            print("Duplicate music player self-destructing!");
        } else {
            instance = this;
            GameObject.DontDestroyOnLoad(gameObject);
        }
    }

Does a static variable serve another purpose that I am unaware about?

This is called a singleton.

This specific singleton is designed to survive from scene to scene. This is why it is flagged to not be destroyed.

It also ensures there is a single instance (hence the name singleton) by destroying itself is instance already exists on Start.

The static variable exists so that you can access it from other scripts if you so desire. It’s just the design of a singleton, there’s always a global access point to the singleton instance.

You might not access it anywhere in code at this point. BUT when you decide to… it now exists to do so. The static variable also exists to track IF an instance already exists, so that you can enforce the singleton aspect of the object.

Here is a unity specific article on singletons. The concept can be applied to all sorts of object oriented development, so instead of giving you a wikipedia or generic article on singletons. I’m giving you a unity specific one. Note this article shows more robust methods of implementing the singleton, the one you showed is a very simple implementation.
http://wiki.unity3d.com/index.php/Singleton

Okay, I’m gonna go ahead and look at that article you provided, I appreciate the response as it was very informative and helpful. Just as another quick question, would that mean that it’s not entirely necessary to use a static variable, that was just so happened to be used in case we want to access it again later? Or was the static part actually necessary.

In the object sense, static means there is only one that is “shared” among all instances of a type of object.

Lets take an object, for example, a shoe.

A shoe might have a variable defining its color. If you have 27 different shoes, they can have 27 different colors.

If the programmer wanted to force ALL shoes to share the same color, generally all they would do is declare the color variable to be static instead of non-static. Then if any shoe changed its colors, ALL shoes colors will change.

Hope that sorta illustrates the concept. There are many valid uses for a static variable. The singleton pattern above is one of them.

1 Like

The static is necessary. It’s what allows if a second instance is created for it to destroy itself, ensuring a single instance.

1 Like