C# Variables in Unity
Objective: Describe common variable data types and access modifiers in C#, and using the Unity inspector, modify the variables.
Variables are electronic containers which store information. The most commonly utilized data types stored are integers, floats, strings and booleans. Each variable can only store a specific data type and this must be specified when the variable is declared. Strings require “” around characters. Floats must be declared using #f. The beauty of a variable is that it is not static; you can change it.
There two access modifiers public and private. Public is accessible by any object and is visible in the Unity inspector on the object to which the script is attached, see below. You can also click on each variable and adjust the variable in the Unity inspector.
Private is available to any method in the current script, but not to other objects. For example, _index is used to track user input, incrementing with each press of the space bar, _index++. Local variables are only available in the method in which they are defined, but not outside the method, even within the current script. The string display is a local variable and used to store output to the canvas text. Also shown is concatenation or the joining of strings using +. The \n denotes a new line in the text.