Declaring a variable and its type
Every variable we want to use in a script must be declared in a statement. What does that mean? Well, before Unity can use a variable; we have to tell Unity about it first. Ok then, what are we supposed to tell Unity about the variable?
There are only three absolute requirements for declaring a variable and they are as follows:
- We have to specify the type of data a variable can store
- We have to provide a name for the variable
- We have to end the declaration statement with a semi-colon
The following is the syntax we use for declaring a variable:
typeOfData nameOfTheVariable;
Let's use one of the
LearningScript
variables as an example; the following is how to declare a variable with the bare minimum requirements:
int ...
Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.