Variables

Learn about variables and how they are used to store and manipulate data of different data types throughout the code.

Introduction

In Unity, variables are used to store values that can be used and manipulated by our scriptA script is a structured set of instructions typically written in a programming language, such as C#. These instructions define specific behaviors and functionalities for GameObjects within a Unity project. In essence, scripts serve as the computational logic behind interactive elements within virtual environments. When applied to GameObjects as components, scripts dictate how these objects behave, respond to user input, interact with other objects, and manipulate the virtual space. They enable developers to implement diverse functionalities, including character movements, object interactions, environmental changes, and user interface behaviors. Through scripting, developers can imbue virtual worlds with dynamic and interactive elements, enhancing the overall user experience in Unity applications.. Variables in C# are defined by their data type, such as int, float, string, and bool, and must be given a name. Given below are some examples of how to declare and use variables in Unity using C#.

Declaring variables

Variables are declared by specifying their data type and name. For example:

Get hands-on with 1200+ tech skills courses.