Variables: What’s in the box?!

Morbid jokes aside, Variables to simply put it are empty boxes that you create to hold a type of data.

For instance, the speed of my car in the game, how much ammunition I have in my laser gun, does an npc hate me or like me, or does it even know what relationship points are? You make a variable to contain some sort of information that can be called by the program so it understands something.

There are four aspects to a variable that you need to remember when you create them. Lets start off by creating variables right inside the public class that we start with. This will make them Global, which means that the entire script knows about it.

Is it public or private?

A private variable is one that can only be accessed within the script itself. Meanwhile a public one can be accessed by other sources. Most variables should be kept private unless there is good reason for it to be accessed. Something such as a score count, or a situation such as a mushroom powerup to boost your players speed.

What type of variable is it?

Now the type is very important as it will determine what actually can go into your variable. The common data types are int, float, string, and bool. There are more data types out there but these four will be your bread and butter.

int: Used for whole numbers

float: Fractional Numbers (Any fractional number needs a “f” at the end! Example: 20.2f)

string: Text

bools: True and False

Now in Unity, there will be others that are used that are Unity Specific such as GameObject, Animator, and Transform that are just as important and I am constantly learning more about. Always keep an eye out for these!

What is the name of this variable?

The third thing needed, is that you have to name the variable you just created. Like a box you put away to storage, take that sharpie and write on it what it contains so you don’t forget. One good thing to note is that it is good practice to begin private variable names with a “_” and also to use camel casing in general. This is where the first word is not capitalized, but any further are. So for example, “_speedForce”.

Is there an initial value?

The fourth aspect for creating a variable is a value. Now it is optional to assign your variable a value as not having one just sets the value at the default such as 0 or false if a bool.

With this now we know how to create them, but what do we do with them, or how can we change them. That is simple!

You can just take the variable name and have it = a new value that matches the data type. (Don’t forget that strings require “”s!) By doing this, my score has changed from 10 to 20 and _name has changed to Joe at the start of the application.

If you want to use the variable in code, you simply once again just use the name.

That will in Unity bring up a message reflecting what my score is.

Public, private, and the Inspector

Now, one last thing and it concerns public and private variables. When you set a variable to be public, it will actually show up in the Unity inspector as a editable value on anything this script is attached to.

So again if these are my current variables on my script.

I look at the inspector and I see this. I see my public variables right here! These are freely changeable but remember that the values in the inspector, take priority over that in the script itself. If need be you can also reset these values to the script defaults by clicking on the hamburger icon, and clicking reset.

Now, if public makes it so easy to change and edit but it is best practice to make most variables private unless needed, can private variables do something similar? Yes they can!

By adding above the variable a [SerializeField], it will force the inspector to show it as well!

A simple but essential part of coding. Variables seem simple, but still require a good amount of practice to master. What I have gone over are just global variables that the entire script can read it, there are also local variables that are specific to a certain part of the script and nowhere else which we can go over at a later time.

It just excites me as there is so much to learn even in what seemed like a simple concept at first.

Once again, thank you sticking with me so far and see you again!




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

Recommended from Medium

Testing Gunicorn WSGI + Meinheld for serving Flask API

Drone Controls w/ New Unity Input System

Flutter Analysis and Practice: RichText Best Practices

Women Soldiers Organization — Week 2/January 5, 2022

5/14 box model (css)

Java Lambda Expressions

Debugging with Xcode and Python time series calculations

Another day at Google Developer Days

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
Edward Kim

Edward Kim

More from Medium

My favorite Anime’s of all TiMe aNd WhY yOu should watch them.

I redesigned the Arcade1up online experience!

Getting Started with Pygame

#CodingisArt: How Martin Eyerer is bridging Tech and Music