🎮 Unity C# Basics: Variables in Scripts

Ques-tlyQues-tly
3 min read

🗒️ My “Sticky Note” Phase

When I started learning Unity, I kept track of player stuff like this:

  • “Health = 100” (scribbled on paper).

  • “Speed = fast?” (question mark and all).

  • “Score = …uh, I’ll figure it out later.”

Every time I tested my game, I had to remember these numbers.

Spoiler: I didn’t

Then I discovered variables
And suddenly, my scribbles turned into code that actually remembered things for me

🧩 What Are Variables?

Think of variables like little labeled boxes.
You can put numbers, words, or true/false values inside them — and Unity will keep them safe until you need them

  • Health? That’s a number box.

  • Player name? That’s a text box.

  • IsGameOver? That’s a yes/no box.

In C#, these boxes have types:

int health = 100;         // whole numbers
float speed = 5.5f;       // decimal numbers
string playerName = "Alex"; // text
bool isGameOver = false;  // true or false

🏃 A Simple Player Script

Let’s throw these variables into a Unity script:

using UnityEngine;

public class PlayerStats : MonoBehaviour
{
    int health = 100;
    float speed = 5f;
    string playerName = "CubeHero";
    bool isGameOver = false;

    void Start()
    {
        Debug.Log(playerName + " spawned with " + health + " HP.");
    }

    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
}

👉 What happens:

  • At the start, the Console prints the player’s name and health.

  • Every frame, the cube moves forward at the given speed.

  • The variables control all of this.

Change the numbers, and your game changes instantly.

🎯 Why Variables Matter

Variables are your game’s memory. They let you:

  • Store a player’s health, ammo, or score.

  • Adjust speed, gravity, or jump height.

  • Save whether a door is locked or open.

  • Keep track of literally anything you care about in your game.

No more sticky notes. Just reusable, reliable data inside your scripts.

🧪 Pro Tip: Public vs Private Variables

You’ll often see public and private in front of variables.

public int health = 100;  // shows up in Inspector
private float speed = 5f; // hidden in Inspector
  • Public → shows up in Unity’s Inspector window, so you can tweak it without editing code.

  • Private → stays hidden, safer from accidental changes.

Think of it like… public = “shared whiteboard,” private = “secret notebook.”

⚡ Quick Cheat Sheet

  • int → whole numbers (score = 10)

  • float → decimals (speed = 3.5f)

  • string → text (playerName = "Hero")

  • bool → true/false (isAlive = true)

🚀 Wrapping Up

Variables are tiny, simple things — but they’re the foundation of everything you’ll build.
They let you stop memorizing numbers, and start making your game world remember them for you.

0
Subscribe to my newsletter

Read articles from Ques-tly directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ques-tly
Ques-tly