C# Static
A very useful keyword in more advanced C# programming. Today I’m talking about the Static keyword!

The Static Keyword:

The Static keyword allows you to specify that a variable should not be unique to each class instance, but instead be the same value regardless of which instance it is accessed from.

In the above example which would be in an “Enemy” class, the “shouldSpawnEnemies” boolean can be though of as a bool that will be the same in every single instance of “Enemy” (on every enemy GameObject in the scene if you’re using Unity). Meanwhile, “_health”, “_lifetime”, and “_name” can have values that are different for every instance of the “Enemy” class.
Singletons:
The “static” keyword can also be used to create Singleton classes (Single class instances that can be accessed from any other script).

By creating a static variable of the same type as the class it’s in, and setting it in “Awake()”, you can create a singleton instance which can be referenced from anywhere. This avoids you needing to hold a reference to the Object it is on, or find it during game-play.

Creating many unnecessary Singletons is often frowned upon and a common mistake though, so you should be 100% sure your class should be a singleton before designing it that way.
Creating unnecessary Singletons can, among many other potential problems, cause strict coupling between classes, meaning that if the “instance” variable is ever “null” then everything fails.
The key point is to understand the Singleton Design Pattern, and know why and how your classes should be designed as Singletons. I’ll discuss the Singleton Design Pattern further in a future post.