Quick questions regarding statics

Am I right in guessing, that if you make a variable in a script a static, you have to use a reference (GameManager > theGameManager.Health), but if it’s an ‘if’ statement, you must use the type name (GameManager.Health)?

Also, is there any way of making private static variables? I’ve tried using SerialisedField, but it doesn’t appear to work. Some of the int variables I have I don’t want to see in the Inspector.

No, if it’s a static variable, you do not access it through an instance of that class.

GameManager.Health is always the correct way.

Just make a private static int variable, but since it’s private it will not be accessible outside that class. It just means that all instances of that class share that variable.

See, this is where statics confuse me. When people say things like ‘you can’t access it through an instance of that class’ it sounds like it can’t be accessed outside of the class/script, and yet they’re supposed to allow you to access variables in other scripts…?

I want to be able to access certain variables in other scripts, so does that mean it must be public…?

EDIT: Ah. That’s what’s confusing me… Something like theGameManager will only work if trying to link to another script’s method. But if it’s a variable it needs to be GameManager.

It means there is only ONE of them, so when you try to access it the compiler knows the difference and corrects you.

For example…

public static class Algorithms
{
        public static float MetersToFt(float meters) { return meters * 3.28084f; }
}

To use the above code you only need to do float result = Algorithms.MetersToFt(5); because the Algorithms class is static, you don’t need (nor can you have) multiple instances of it.

However if you have a need for a class with multiple instances then it cannot be static.

Compare that with something like a CAR class. You could have hundreds of cars in the scene so when you have code trying to talk to a car you are required to specify exactly which instance of that class you are talking to.

    public class Car : MonoBehaviour
    {
        public void DriveThisCarTo(Vector3 position)
        {
            // ...
        }
    }

Therefore Car can’t be static, because there could be many difference instances. Talking to a car would look something like this:

Car thisSpecificFreakingCarRightHere = GameObject.Find("CarNumber33").GetComponent<Car>();
thisSpecificFreakingCarRightHere.DriveThisCarTo(Vector3.zero);

This is a conversation of “object identity”.

You have a class (script, component, whatever). This is where the methods/members are defined for you object type.
Then you have an object instance of that class. There can be multiple instances of any given class.

For example there is the class ‘GameObject’, but you have hundreds, if not thousands of GameObjects instances in your scene. Each instance is distinct from one another, and their instance members are therefore distinct. The ‘name’ of one GameObject is distinct from the ‘name’ of another.

Declaring something static associates a member with the class rather than the object.

There is only ever 1 class ‘GameObject’, though there are multiple instances of it. (yes technically you can have 2 classes named GameObject in different namespaces or assemblies, but technically they’re different classes since their full names are different).

As a result you don’t access it via an instance, but by the class directly.

The fact that there is only ever one is a HUGE design choice and you should be careful about.

For example a static variable ‘GameManager.Health’ implies only one health ever existing! Whose health is it? Is it the player’s health? What if you decided to implement multiplayer?

But yeah when people say:

They’re not talking about the code within the class.

They’re talking about having a reference to an instance of that class.

var obj = new GameObject("");
obj.name = "A Name"; //I just accessed the name through the instance of the class GameObject
var foundObj = obj.Find("Something");//this will fail, Find is a static member of GameObject, I just attempted to access it via an instance of that class

10/10 for variable name choice… would definitely instance again.

3 Likes