Variables!

Think of variables as a storage container that stores things. The things we store can change and/or vary. A variable stores different types of data.

If you do not already have a script created for following along, you can check out my previous article, “Creating a Player Object and Adding Simple Player Movement in Unity”. That article shows you how to create a Player script. This script will be all you need to follow along with this article.

Let’s create a variable. There are different types of variables, which we refer to as ‘Data Types’. As the name implies, each type holds a specific type of data. Below is a list of some common data types.

'Integer' (int)   - Whole numbers (i.e.; -2, 0, 296)
'Float' (float) - Fractional numbers (i.e.; -2.7, 0.0, 4.26314)
'Boolean' (bool) - Logical True/False (i.e.; true, false)
'String' (string) - Sequence of characters (i.e.; "I am a string.")
There are many more. These are only a few of the more common ones.
Other examples include: (double, long, short, byte, char, etc.)

My last article discussed player movement and speed. So, for this article we’ll create a ‘speed’ variable.

There are a few things to consider when creating our variables.

  1. Whether the variable will be a ‘public’ or ‘private’ reference
    Public means other scripts and game objects know the variable exists.
    Private means only the Player knows the variable exists
  2. Which ‘data type’ to use
    (int, float, bool, string)
  3. Name of the variable
    All variables must have a name.
  4. Value assigned
    An assigned value is optional.
  • First, we’ll type the following pseudocode into our script. Place this pseudocode above the
    “// Start is called before the first frame update” comment line.
// public or private reference
// data type (int, float, bool, string)
// name of variable
// assign optional value
  • Save your script.
Type Pseudocode In Script

Declaring a Speed Variable

Now we will follow the pseudocode we typed in previously.

  • Because this code does not need to be seen or manipulated by other scripts or game objects, we will make the speed variable ‘private’.
  • We want to be able to change the value of the variable in fractions or decimal places, so we will use the ‘float’ data type.
  • We need to give the variable a name. We will use the name ‘_speed’.
    It is common practice to use an underscore “_” in front of ‘private’ variable names.
  • Optionally, we can assign a value to the ‘_speed’ variable. We will assign 3.5 as our value. When using a variable of data type ‘float’, values must end with the letter ‘f’. So our value would be ‘3.5f’.
  • Enter the following line of code into the script under your pseudocode:
    private float _speed = 3.5f;
  • Save your script.
Type Speed Variable into Script

In Unity, you or a team member may want to change the value of ‘_speed’, for testing purposes, without having to hard code it in the script every time you want to change it. With the variable set to private, you will have to use the ‘[SerializeField]’ attribute to allow access to the variable within the Unity editor. The variable would still not be accessible by other scripts or game objects.

  • To do this, you would simply add the following line of code above the variable line of code.
    [SerializeField]
  • Save your script.
Add [SerializeField] to Script

This will add the ‘_speed’ variable to the Inspector panel of the game object to which the script is attached.

Note: If the value is changed inside Unity, that value supersedes the value written in the script. 

The final step would be to make use of the variable. If you have the script from my previous article, “Creating a Player Object and Adding Simple Player Movement in Unity”, change the following line from
transform.Translate(Vector3.right * 5 * Time.deltaTime);
to
transform.Translate(Vector3.right * _speed * Time.deltaTime);

Now you can manipulate the speed of the Player object from within the Unity editor.

Thank you for taking the time to read this article. I hope it was informative and helpful.

--

--

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
James Hills

James Hills

I am a married father of 5 children. I decided at 13 years of age that I was going to become a Software Developer.