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.

Example of part of an Enemy class

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).

Create a Singleton class instance

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.

Referencing a singleton

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.

--

--

--

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Gentle Introduction to Dependency Injection

Streaming Response from Python Django Application

Being an iOS Developer Academy Graduate

Using the async-hooks API to integrate Envoy with our RPC library — Part II

New blockchain based copyright protection, new defender service, unlimited plans and more!

Easy Schema Migrations in .NET Core

Originally published at https://adamtheautomator.com on August 4, 2020.

Scatter Plot using R

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

More from Medium

C# Refreasher: Dictionaries!

Unity C#: Lists

Ascend to the next level with Higher Order functions

Interfaces vs Abstract Classes