Using Variables in Unity & C#

Erin Kerr
3 min readAug 16, 2022
Photo by Erda Estremera on Unsplash

To start, let’s define what a variable is and what it does for us.

A variable is just a container or box for a piece of information. A variable gives you a quick, descriptive reference to a value or entity to use in place of the actual value or entity — which makes it easy to adjust the value if needed.

Example of hard-coded speed value

Imagine you were building a game where your player moves around the screen at a speed of 5. So, everywhere in your program that you calculate your player’s movement, you would need to enter the value of 5 for the speed. Then, a while down the road, you playtest your game and realize that the player moves a little too slow for the feel of the game. You would need to go through your code, remember everywhere that a “5” is referring to the speed and update it to the new value.

Example of a speed variable

Let’s run through this again using a variable: Instead typing the actual speed value of 5 in every place needed throughout your program, you define a variable called “speed”. Now, when you go back to your code to update the speed, you only need to update the value once and every time “speed” is referenced, the new value will be used.

As a bonus, using a variable will allow your speed value to be dynamic. If you wanted to add a power-up to your game that increase your speed for a time, you can update the value of speed within your code!

How to use a variable

First, you have to declare your variable. This is where you define:

Examples of variable declaration
  • Access Modifier: Who or what can access the variable.
    Typically public (allows other parts of the program to access the variable) or private (only accessible within the part of the program where it is declared)
  • Data Type: What kind of data is stored in the variable.
    This can be a standard data type, like int (integer), float, string, or bool (boolean). This can also be an object or component
  • Label: What is the variable called.
    Typically, a variable name should be relatively short, but descriptive and meaning. Additionally, for C#, the .NET standard dictates private variables should start with an underscore (_).
  • Initial value: Optionally, what is the starting value for this variable using an equal sign (=) as the assignment operator.
Example of referencing a variable in code

Once the variable is declared, you can reference it in code simply by its label. When this code is processed, it will insert the value for _name, outputting “My name is George” to the console.

Example of updating the value of variable after declaration

Similarly, if you need to update the value of the variable after declaring it, you can reference it in code by label and use the assignment operator to assign a new value. In the example above, the variable _name is updated to “John”, so the output would be “My name is John”.

Variables are a fundamental part of programming, allowing for code that is easy to read and understand, due to descriptive labels, as well as making code easier to maintain (one change to correct the value throughout). Additionally, variables allow for dynamic code that can be re-used in a modular fashion.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response