Static And Non-static?

What is the difference between the two variables below.

public static int lives = 4;
public int lives = 4;

I know the first one is static and therefore the value is the same across all scripts. But in my experience the other is also the same. So why insert static. Is the second on static by default?

It’s important to understand that this line of code isn’t just setting up the variable’s permissions and data type. It’s defining the starting value of the variable. When an instance of a class is created all non-static variables in that copy will be set to their default values.

If you check the value immediately after it’s been created, it will always show 4, because that’s the default you set.

No, and you can prove this very easily. The following script, when placed on two game objects, will report the exact same value for X because it’s a static variable, but report different values for Y because each copy of the script will have its own copy of Y.

At least assuming the random number generator doesn’t spit out the exact same number. If it does, just run it again. :stuck_out_tongue:

using UnityEngine;

public class StaticTest : MonoBehaviour {
    public static int x;
    public int y;

    public Start() {
        x = Random.Range(0, 10000);
        y = Random.Range(0, 10000);

        Debug.Log("X is " + x + " and Y is " + y);
    }
}
4 Likes

I’m pretty sure it won’t (assuming the random number generator doesn’t spit out the exact same number). :slight_smile:

1 Like

Good catch. If I had placed the init lines into Awake() and the Debug.Log into Start() it would have. Fixed code below.

Edit: Oh, and better yet, I forgot to specify their return types. Silly mistakes always happen when I post untested code. :stuck_out_tongue:

using UnityEngine;

public class StaticTest : MonoBehaviour {
    public static int x;
    public int y;

    public void Awake() {
        x = Random.Range(0, 10000);
        y = Random.Range(0, 10000);
    }

    public void Start() {
        Debug.Log("X is " + x + " and Y is " + y);
    }
}
2 Likes

A static variable is a class-level variable. Any instance of the class it is associated with will essentially be pointing at the same variable. The version that isn’t static is an instance-level variable. It is technically different for every separate instance of the given class.

If you create two instances of the class, each of those different instances can have their own individual value for the non-static variable. If you change the value on one, it will not alter on the other. That is what it means to be an instance.

But for the static variable, that is not so. If you change the static variable on one, it will change for the other. The variable is associated with the class, not with the instances of the class. This makes the static variable very accessible, but it makes it less unique.

Static variables are used when you need the same variable to have the same value across all instances of a class. Non-static variables are used when each instance of a class needs to have it’s own unique value.

In the case of the original example, both variables start off as being 4. In that, they are functionally identical. But if you try to change either of them, that is when you would start to see the difference. The static version would change the value for every last instance of the script currently running in the scene. But the non-static version would be unique to it’s instance, every instance of the script could have the value of that integer changed to be a separate value. You don’t see the difference in the script, but you do see it when you run the script in the preview, and then try to change the values in the inspector.

4 Likes

static = spooky action at a distance.

1 Like