Variables! The Building Blocks of Programming

What are variables?

Variables are buckets of data. A variable holds a singular type of data for a specific purpose and you can access or update the data value stored in a variable through code.

How to use them?

To create a variable in your C# script in Unity, you need the following:

  • Access modifier
  • Data Type
  • Variable Name

Choose level of Access

When creating a variable, you have to set the level of access. Should any other part of your project have direct access to this variable or just this script? The two main levels of access are:

  • Public: Any and every part of your project can directly access this variable; Variable is also viewable in the Inspector in the Unity Editor by default.
  • Private: Variable can only be accessed within this script

In general, it’s best practice to keep your variables private unless there is a specific need for broader access. Even then, there are often other ways to access a variable from other scripts while keeping them private.

SerializeField attribute

If you need to access your variable in the Inspector, but do not need to allow access to other scripts, you can add the SerializeField attribute to your private variable.

SerializeField attribute

To do this, enter SerializeField in square brackets on the line above your variable declaration.

Choose your Data Type

When creating a variable, you need to determine what kind of data will be stored in that variable. Four of the elementary data types are listed below, along with the keyword you’ll use to refer to them in your script:

  • Integers int: Whole numbers with no decimals (ie. 27)
  • Float float: Numbers with decimals (ie. 4.8f)
  • Boolean bool: True or False
  • String string: Characters, such as letters, numbers, and special characters (ie. “abc123!!”)

You can also store the reference to an object or a component in a variable by setting the data type to the name of the object or component (ie. Transform).

Choose your Name

Lastly, to create a variable, you need to name it. To make it easier to read and debug your script, be sure to use descriptive and unique variables names. Also, it is common practice to add a notation to your variable name to identify private variables. Depending on the coding standards you follow, you can precede your variable name with an “m” or an underscore ( _ ) when it is a private variable. This will also make it easier to identify private or public variables throughout your script, without referring back to the line where you created the variable.

Tying it all together

To actually create a variable in your script, initialize it in following format:

access-modifier data-type-keyword variable-name

Examples of initializing variables

To assign a value to a variable, you use the equal sign (=), which is called the assignment operator. The argument (or variable) to the left of the assignment operator will take the value provided to the right.

Examples of assigning values to variables

You can assign a value when you initialize the variable, but it is not required. If no value is assigned when the variable is initialized, it will take on the default value for the data type (ie. 0 for integers and floats).

Why are variables so important?

Variables allow you easily change the behavior or outcome of your scripts. You can reuse code with different inputs to get different outputs. Variables can also help describe what your code is doing. Take this Player script excerpt for example:

Excerpt from Player script showing _speed variable

With speed as a variable instead of a hard-coded number, I can easily adjust the value of speed to change how the player moves without editing the Player script:

Adjusting the value of the Speed in the Inspector

If speed was not a variable, I would have to go into the script, find the line where the speed was hard-coded and update the value from 5 to 10. Then again, from 10 to 25.

Example of line with speed as a hard-coded value

In a long script, it would be hard to remember that 5 is the speed value. And if I was using the speed in any other lines, I would have to manually update each line. By using a unique and descriptive variable name, I know the purpose of the value and can easily locate other uses throughout the script by searching for variable name.

If that doesn’t convince you, I could also reuse the Player script to create multiple Player objects and set each with a different value of Speed in the Inspector:

Example of multiple Player objects with different Speed values

Without the variable for speed, I would need to write a different script for each player with the different speed values hard-coded.

If you haven’t caught on yet, variables are cool (kind of like fezzes).




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

Recommended from Medium

Ulord Project Progress (From July 8, 2021 to July 14, 2021)

Amazon S3 is Better Than Creating Your Own Image Database

Scala+Gradle+Cucumber = A holy alliance


How SwiftMQ HA solves a Split-Brain automatically


#4 Analytics — Loading Data into Dask and Pandas

Three criteria for selecting the right enterprise CMS

Google Shared Drive and Workspace SFTP and FTP

couchdrop secure file gateway sftp shared drive

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
Erin Brown

Erin Brown