---
title: "Introduction to variables"
content_type: tutorial
url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables?language=en"
language: en
container_type: course
container_title: "Creator Kit: Beginner Code"
container_url: "https://learn.unity.com/course/creator-kit-beginner-code.md?language=en&version=2022.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/introduction-to-variables.md?language=en&version=2022.3"
previous_title: "Get started with code in Unity"
previous_url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/get-started-with-code-in-unity?language=en&version=2022.3"
previous_markdown_url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/get-started-with-code-in-unity.md?language=en&version=2022.3"
next_title: "Write your own instruction"
next_url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/write-your-own-instruction?language=en&version=2022.3"
next_markdown_url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/write-your-own-instruction.md?language=en&version=2022.3"
difficulty: beginner
duration_minutes: 10
unity_version:
  - "2022.3"
supported_unity_versions:
  - name: "2022.3"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2021.3"
  - name: "2020.3"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2020.3"
  - name: "2020.2"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2020.2"
  - name: "2020.1"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2020.1"
  - name: "2019.4"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2019.4"
  - name: "2019.3"
    url: "https://learn.unity.com/course/creator-kit-beginner-code/tutorial/introduction-to-variables.md?language=en&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/introduction-to-variables.md?language=en&version=2019.2"
last_updated: "2026-08-04T14:24:29.000Z"
---

# Introduction to variables

Learn what variables are, how to read them in scripts, and prepare to define your own variable values.

In this tutorial, you’ll:

- Learn what a variable is and why it is useful
- Identify a variable within a script
- Identify the value assigned to a variable
- Practice reading instructions in a script

When you’ve finished, you’ll be ready to create your own variable.

## Working with the Creator Kit: Beginner Code scripts 

[Video](https://connect-mediagw.unity.com/h1/20191119/videos/c5eb1eb2-936b-4d59-a2e1-2823e776942e_Unity_HOC_1.2_Variables.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20191122/2414bb8d-52bd-43f0-a986-eb94ab6c387f_UnityHOCVariables.vtt))


In this tutorial, you’ll identify specific instructions in the **SpawnerSample** script.

Remember:

- You won’t be able to understand everything in the script straight away.
- It may take you a little time to find information in the file.


This is completely normal. In this Creator Kit, you’ll learn how to approach an unfamiliar script and identify key elements, rather than how to understand everything in game-specific scripts.

## Find an instruction in the script

In your code editor, find the **10th line** of the script:

```csharp
int angle = 15;
```

This is the base building block of programming: an **instruction**. An instruction is a single task you give to the computer.

The instruction ends with a semicolon (**;**). This tells the compiler that the instruction is finished and the computer should now complete it. Compilers don’t care about the ends of lines — a semicolon is what signifies the end of an instruction for them. However, humans also need to be able to read C# code too; to improve readability, it’s good practice to put each instruction on a separate line.

Before you progress to the next step, consider:

- The purpose of this script in the game
- The word ‘angle’

What could this instruction be telling the computer to do?

## Introduction to variables

The instruction  **int angle = 15;**  declares and assigns a variable.

A **variable** is a container that stores a value. You can think of it like a trunk with a name written on it, where you can store information you want to retrieve later. As its name suggests, the information stored in the variable can change (or vary).

Before you can use a variable, you need to **declare** it by stating its name and type. You can also assign it an initial value.

In the image below, a variable (trunk) called **gems** has been assigned the value **2**.

![](https://connect-mediagw.unity.com/h1/20190808/learn/images/e7e127f0-b5cd-43e2-9583-72ca5b943c57_image5.png)

## Identify the type and name of the variable

There are two things you need to identify when **declaring** (or creating) a variable:

**1.**  The **type** of information that will be stored, which tells the computer how much space to save in its memory to store the information. All variables need a type.

This is an **int** variable. These variables store integers (whole numbers).

**2.**  The **name** of the variable, which you will use every time you want the computer to retrieve the variable’s value.

The name of this variable is **angle**.

##  Identify the value assigned to the variable

You can **assign** a value to a variable using the equals sign (**=**). This means that you copy the value to the right of the equals sign into the variable on its left.

**int angle = 15;** tells the computer to store the integer value **15** inside the variable called **angle**.

Every time you use the variable **angle** in your code, the computer will substitute the integer value.

## Identify where the variable is assigned a new value

In the SpawnerSample script, find the next **two** lines where the int variable **angle** is assigned a different value.

In each line, what is different from the first time the variable was assigned a value?

## Review the two instructions 

The angle variable is assigned different values on lines **17** and **22** of the SpawnerSample script.

Unlike the first time a value was assigned to **angle**, its type is not included. This is because the computer already knows that the variable exists and how much memory space it needs.

Since instructions in a script are sequential (completed one at a time), the changed value for the variable will only be used in instructions **after** it has been assigned.

In the image below, the example variable **gems** has now been assigned the value **4**.

![](https://connect-mediagw.unity.com/h1/20190808/learn/images/aaa58c20-7c63-484c-8f42-ccbd2ed25d6c_image10.png)

## Tutorial summary

In this tutorial, you learnt:

- That scripts are made up of instructions
- That variables are containers for values in code
- How to declare and assign a variable
- That one variable can have different assigned values within the same script, because the computer executes instructions sequentially

In the next tutorial, you’ll apply your learning and write an instruction to declare and assign a new variable.
