My journey becoming a Unity game developer: Variables-the building blocks of programming

Different types of variables demonstrated.

What is a variable?

Well, there are a couple of ways to answer this with the 1st answer being a variable is the most basic part of programming where everything starts with them, and without them programs can’t exist.

DataType uniqueName = value;

DataType is the type of data the variable will store and must be specified. UniqueName must be the name of the variable, and different from keywords used in C# to prevent conflicts. Value must match the specified Type if there is a value assigned. Finally, the variable declaration must end with a semicolon to complete the statement. Example: int currentAge = 21;

accessModifier dataType uniqueName = value;

There are 4 main Access Modifiers used in C#, but the 2 beginners will use most often in Unity are Public which is available to any script without restriction, and Private which is only available in the class they’re created in called the Containing Class. Also, any variable without an access modifier defaults to private.

int- which is a simple integer (ex. 8)

float- is a number with a decimal (ex. 3.14)

string- characters inside double quotes (ex. “This is it!”)

bool- a boolean decision, either True or False

Another thing we can do with variables is using Type Conversions on them. There are 2 types of conversions with Implicit conversions taking place automatically like when a smaller value will fit into another variable type without any rounding. (ex. float implicitConversion = 5;)

  • Global — refers to a variable that can be accessed by an entire program which in this case is the game.
  • Class or Member — refers to a variable that is accessible anywhere within its containing class.
  • Local — refers to a variable that is only accessible inside the specific block of code it was created in.

--

--

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
Rhett Haynes

Rhett Haynes

Learning to become a Unity game developer.