Variables in programming

Variables

The building blocks of programming

Variables are like droors

When programming, there are these “things” we have to use, yes, have to, called variables. Some will say they are like droors or boxes that contain certain information. To generalize, they are containers which hold temporary information used by the program which can change variably.

Variables must be declared within the code as follows:

Typical variable declaration

Variables can be declared as public or private.

Public Variables

When declared as public, a variable can be accessed and modified by other script in the program.

Private Variables

When declared as private, a variable can only be accessed by the script in which it resides.

Variables can contain several types of information. Some of the most common types are as follows:

  • String : These variables will contain a sequence of characters or letters. “Hello”
  • Int: integers are whole numbers, they don’t havedecimal places. 25
  • Char: character variables will hold only one character. “A”
  • Float: floats are numbers with decimal places. It has a precision of 32 bits. It is important to note that when assigning a value to a float variable, it must be followed by a lower case f. 3.14f
  • Double: doubles are like floats but can hold a larger number of decimals (64 bit precision) and take up more memory than a float. They don’t need the “f” after the number assignment.
  • Bool: booleans are either true or false. They answer yes or no questions. Is the light on = true, is the light off = false

Although there are several ways to write a variable name, it is recommended that variables have a descriptive name, this way it is clear as to what it does.

Naming variables

We normally use cammel case when writing a variable name. This is where we always start with a lower case letter but the first letter of the second and/or third, etc, is capitalized:

Camel Case

For private variables, convention dictates that they start with an underscore. This makes themeasier to spot inside the code.

private variable naming convention

Like the name implies, the values stored in variables can be changed. For example, if you are keeping score in a game, it will increase or decrease over time. If you have an inventory of items, it will also change throughout the game. This is called reasigning a variable.

Reasigning a variables’ value

As we can see, variables only need to be declared once. After that we only need to reference the variable name and give it a new value that meets its type.

Even though Unity has other variable declarations, these are the most common types used. We will most likely see the other types later on.

--

--

--

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.

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

Recommended from Medium

Overcoming Parquet Schema Issues

Best APIs For Doing Marketing Segmentation Faster

Reaktive binary compatibility: how we achieve it

Progress on Senior Project Deliverables

What Happens When You Type in ls *.c?

Organize everything of your company with an all-rounder software

Why you should use a window manager

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
Marc Frappier

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.

More from Medium

Variables

How to check assemblies types in Unity C#

Script Communication in Unity using GetComponent

Scriptable Objects 1

Scriptable Object Script