Variables! The Building Blocks of Programming

Andrew Crippen
4 min readApr 7, 2021

--

Variables are basically containers for data or hold a reference to data.

The 4 most common Data Types are strings, bools, floats and ints, all variables will have a Data Type. In C# and Unity you can use any type of component as a Data Type and store a reference to it in a variable which can then be accessed and modified within code.

string - A collection of characters, most commonly a word or words but can be any random combination too

bool - A bool’s value can be true or false

float - A float is a number that has numbers after a decimal like 50.62 75.34

int - An int is a whole number like 1 2 3 4 5 20 37 100

To declare a variable every variable needs an access modifier, a data type, a name and optionally a value can be set/initialized when when it is declared.

Access Modifiers

Access modifiers are what determines what level of access other classes/scripts have to the variable. The most common are public, private and protected.

public - Anything can access it.

private - Only the class in which it is declared can access it.

protected - Only the class in which it is declared or classes which are derived or inherited from the class that declared it will have access.

Public access modifiers also allow it to show up in the inspector in Unity to be modified, if you use a [SerializeField] attribute above private or protected they will also show in the inspector in Unity and be able to be modified.

As you can see the public and serialized fields are showing in the inspector.

Data Types

Data types can be any of the 4 common ones listed above or any number of different ones within the Unity api.

For example you could use a data type of Transform to reference a gameObject’s transform and then through code you will be able to modify the transform’s position, rotation and scale. You can use a public access modifier or serialize the field of a private one and then simple drop the gameObject into the field in the inspector and it will automatically add the transform. This works for other components in Unity too including scripts.

Names

A variable’s name should be in camelCase, first letter of first word lower case, first letter of any other words capitalized. If it’s a private variable it should start with an underscore _ which is the .net C# standard, the underscore allows you to easily know you’re looking at a private variable if you’re deep into code 1000 lines down. If you aren’t declaring the value then the name should end with a semicolon ;

Values

The value of some variables can be set/initialized when it is declared by adding the = sign and then setting the value. For a bool the default value is false. A float should always have an f on the end of it. Vector3 should start with new . Always end a variable line with a semicolon.

Using the variable within code

Now that you have a variable you can change it’s value within code or if it’s a data type that is a reference to something like a Transform you can access it in code by typing the name followed by a period and have a whole list of options within Visual Studio or your IDE of choice.

As you can see variables are what you use to do pretty much anything within code.

This was just a brief overview of variables, there are many different data types, more access modifiers and even more different attributes but what I have covered are probably the most commonly used with Unity.

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

--

--

Andrew Crippen
Andrew Crippen

Written by Andrew Crippen

Unity Developer/C# Programmer for VR, 2d,3d Games and other Immersive Interactive Experiences

No responses yet

Write a response