What are Static types and how do you use them? Let’s explore these questions and more!
For starters, you need to use the keyword static to make a static type.
Classes, variables and methods, can all be made static. Unlike instances, Static types are stored in the computer memory for the life of your program, so use discretion as far as when you really need to use them.
With static variables, you don’t need to store the desired script as a variable, nor do you need to use GetComponent to communicate with it. All you need to do is call the static variable at the Class level.
Let’s do an example using a points system. Create an empty object in the scene view and title it Points Manager.
The Points Manager object gets a new script component called Points.
I also create a script called Points Trigger and attach it to the camera. I will use this to increase the point total with player input.
This is a simple public class with a single int variable to store the points value.
The Points Trigger script has a variable to store the Points script. In void Start, I use the Find command to search for the Points Manager, and then GetComponent to grab the Points script. In void Update, the user input is checked for the space key to be pressed, and then it will add 10 points to the point total.
Here is a clip of me ramping up the points with the space key. Points are viewable in the inspector because they have a public access modifier.
Let’s go back to the Points class and add the keyword static, between the public access modifier, and the int value type.
In the Points Trigger, I now have an error where I am adding points to the total. This is because I now need to call the Points script at the class level.
Here is the huge benefit of using static types. I can comment out the variable for the Points script, as well as the GetComponent call. I can simply call the Points script at the class level, and then use dot notation to reference the points variable, and add to it.
Something important to note, is that even though the points variable is public, it is no longer visible in the inspector.
The points can still be monitored by adding a Debug message, sending the point total to the console whenever I add to it.
Here are the points still adding up in the console.
Static methods work in a similar way. Here I pass in the amount of points to be added via method parameters, and then add that to the points variable.
Simply call the Points script at the class level, and then ping the desired method.
Let’s make the Points class static. There is immediately an error under MonoBehavior. This is because static classes can not use class inheritance.
Another thing to note to avoid errors, is all variables and methods within a static class, need to also be static themselves.
Static vs Instance Members
Here is something very important to note when comparing static members to instance members. Static members are shared across all instances, while instances are copies. Instance copies are temporary, while Static members remain permanently in the program’s memory.
Too many static types could potentially take up too much of the computer’s memory, so it is important to use them wisely. One example would be a High Score in a game, or in this example, an int value for the total number of items being created.
Whenever this constructor method is called, the total items variable will increment by 1 for each item that is created.
Utility / Helper Classes
Utility classes can be used to hold methods that you might want to call repetitiously. Because they are static at the class level, they can’t use class inheritance, and all variables and methods inside must also be static.
Let’s make a Utility Helper class, as well as a method to change the color of a game object.
Here in a Player script, I check the space key for user input. When pressed, the Utility Helper is contacted, and the Change Color method is called.
The Player script is attached to this cube object, so it turns blue when the space key is pressed.
If we want to override the inserted color and randomize it, just reassign the color to hold random value assignments in the RGB slots.
Here is the cube changing to random colors when the space key is pressed.
Just like with classes, you can also use static constructors. The static variable will be applied to all shared instances.
What is important to note here, is the order of operations in which these methods are called. I have a debug message sending to the console in the public Item constructor, and then another in the static item constructor. Typically, the public method debug would be called first, but it doesn’t go like that with static methods.
As you can see here, the static method is called first, followed by the public methods.
That’s it for static types. Thanks for reading!