๐Ÿ  Home

Variables in Unity

Variables are a way to store information in scripts. They allow you to define values the game can use โ€” for example, player speed, number of lives, or a characterโ€™s name. Thanks to variables, game objects can remember and change their state.

Each variable has a name โ€” this lets you refer to the value in code. Variable names can include letters, numbers, and underscores, but canโ€™t start with a number or match a reserved keyword. Developers usually pick clear names like score or playerHealth. Variables can be declared inside methods (available only in that block), or at the class level โ€” which makes them accessible throughout all methods. For instance, a variable like tmp might be used inside Start for temporary data, while a class-level variable can store persistent info such as player health or a reference to another object.

Itโ€™s also important who can access the variable. If itโ€™s marked public, the value can be edited directly in Unityโ€™s Inspector โ€” great for tweaking without changing code. You can also access it from other scripts. If itโ€™s private (the default), itโ€™s only accessible inside the script itself.

๐Ÿ’ก If you donโ€™t specify an access modifier, the variable is private by default. That means itโ€™s only visible inside this script. Still, writing private explicitly is a good habit โ€” it makes your code easier to read.
PS: Then again, who really enjoys the extra work of typing private? ๐Ÿ˜Š

Next, weโ€™ll look at the main types of variables most commonly used in Unity.

int

int is used to store whole numbers โ€” without decimal points. These variables are useful for tracking scores, number of lives, ammo, and other numeric values.


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Integer variables
    public int score = 0;
    public int lives = 3;
    private int health = 10;
    private int bullets = 30;
    // also private
    int flowersInTheBag = 1;

    // Start is called once when the object starts
    void Start()
    {
        int tmpHealth = health;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
  

float

A float is used to store numbers with decimal points. These variables are useful for values like speed, damage, timers, or coordinates. In Unity, float is often used for movement calculations, and in physics or animation systems.


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Floating-point variables
    public float speed = 5.5f;
    float jumpHeight = 2.2f;
    float damage = 1.75f;
    float timeToExplode = 3.0f;

    // Start is called once when the object starts
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float tmpSpeed = speed;
    }
}
  

bool

A bool (Boolean variable) can only hold two values: true or false. In the computer's memory, this is usually represented as 1 and 0 respectively. Boolean variables are useful for checking states โ€” for example, whether the player is alive, a button is pressed, or a light is turned on.


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Boolean variables
    bool isAlive = true;
    bool hasKey = false;
    bool isGamePaused = false;

    // Start is called once when the object starts
    void Start()
    {
        bool tmpAlive = isAlive;
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}
  

string

A string is used to store text โ€” words, phrases, or characters. These variables are useful for player names, messages, dialogue lines, and any other string-based data. Text values are wrapped in double quotes: "...".


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // String variables
    string playerName = "Kiava";
    string greeting = "Hello, player!";
    string levelName = "Forest Valley";

    // Start is called once when the object starts
    void Start()
    {
        // Output to console: Hello, player! Your name is: Kiava
        Debug.Log(greeting + " Your name is: " + playerName);
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}
  

Vector3

Vector3 is a variable type used to store coordinates in 3D space โ€” along the X, Y, and Z axes. By default, Unity works in 3D, where X is left-right, Y is up-down, and Z is forward-backward.

If you're making a 2D game, Unity still uses a 3D scene โ€” the Z axis often just stays zero. For convenience, there's also a Vector2 type, which only contains X and Y.


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Vector variables
    Vector3 startPosition = new Vector3(0f, 1f, 0f);
    Vector3 jumpForce = new Vector3(0f, 5f, 0f);

    // Start is called once when the object starts
    void Start()
    {
        transform.position = startPosition;
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}