You put the information in (the box) you take the information out (the box) and you shake it all about.. Variables and their usage (less the hokey pokey).

So.. what exactly is a variable? Well the best explanation that I’ve been given (and I’m sure a lot of people) is a variable is basically a box that you can put information inside, manipulate it while it’s there and take it out to use it.

COMMON VARIABLE TYPES

These are the common variables you will use:
float: This type holds a number that can be a decimal. float someFloat = 1.5f;
*Note* You have to put an f at the end of the value when using a float.
int: This type holds a number but only a whole number. int someInt = 1;
string: This type holds characters. string someString = “Hello”;
bool: This is a true or false only. bool stomeBool = false;
GameObject: This holds an Object. GameObject someOjbect;

So those are the basic variables. There are more but for this tutorial we will just teach these. You can read the Unity Manual for more types.

PRIVATE OR PUBLIC

Private: When you make a variable private you are basically saying that this variable can only be used the class they are created in. Private variables cannot be seen inside the Unity Editor unless you add [SerializeField] before it. This will allow you to keep it private but use it in the Unity Editor.
Public: When you make a variable public you are opening it up to manipulation from anywhere in the game. Other scripts for example can reach in and do something inside of this variable. Public variables can also be seen inside the Unity Editor.

Example:
public float someFloat = 1.5f;
private int someInt = 3;
public string someString = “Hello”;
private bool someBool = true;
public GameObject someObject;
*Note, when using a GameObject variable unless you set it in the code you will need to give it an object in the Unity Editor*

HOW TO USE VARIABLES

So let’s say you are making game (Yay you!). You make a player.
You give that player an age, strength, name, movement speed, poisoned.
So you have these 5 characteristics of the player. Now, we will need to use these throughout the game as follows:
Name: The character will be given a name that NPCs will use to talk to him.
This would be a String variable because you will be using characters:
private string playerName = “Laine”;
Age: This will change over time and the player will get older.
private int playerAge = 10;
Strength: This will be called on to see if a character can lift something.
private int playerStrength = 18;
MovementSpeed: This will tell us how fast the player can move.
private float moveSpeed = 3.5f;
Poisoned: This will tell us if the player is poisoned or not.
private bool playerPoisoned = false;

Naming Conventions

Note the way I named the variables. We call this CamelCase. Where you do not capitalize the first word but capitalize the second. I prefer this, I just like the way it looks. But you can also use _ such as move_speed or player_strength.
The other thing is to make sure you be as descriptive as possible. You may be writing a game for months even years and you might come back to a script you haven’t set eyes on in a long time. You want to be sure you understand what it’s saying.
NO:
private int st= 4;
YES:
private int playerStrength = 4;

Tha.. tha.. that’s all folks!

So that should give you a bit of a look on how to use variables. In the end it’s up to you to come with a naming convention that suites you. But remember, you might not be the only one reading your code, so try and keep it simple to understand!

Tomorrow we will delve into something called pseudo code and why we should use it. You won’t want to miss this one because you really should be using it to keep your code readable!!

Happy clicking!

With Unity all can be accomplished!