Variables as building blocks of programming (Unity3D & C#)

Alberto Garcia
2 min readNov 23, 2022

--

In the case of Unity and C#, variables are containers of information that require a name, an individual data type, and a public or private reference. There are four types of information that can be stored in these variables: a “string” of text that is stored between quotations, a true or false statement called “bool”, an integer set as “int” to store whole numbers, and “float”s that store numbers with decimal place, good fore more precise data.

Four different data types: string, float, int & bool.

A variable is declared first through its privacy state by writing “public” or “private”, followed by the data type and finally its name written in “camelCasing” (first word in lowercase and consecutive words with first letter in uppercase as an industry standard). These are the requirements to declare a variable. The value assigned to each variable after the “equals” sign (=) is optional and may be declared at the beginning of a script or through the different methods. This optional factor is the information that will vary throughout game play and altering the value is the magic that coding runs on each program.

Look at variables on the Editor; speed var is hidden because its set to private.

For example, if after four levels accomplished in the game, the character Jack was to turn 18 years old, there could be a function that sets the bool isAdult to true and in return Jack’s speed becomes “7.5f” to run faster than kids. Variables often rely on each other to execute functions for specific results; some containers are for bread and others for peanut butter, but the magic occurs when both intertwine.

adding a serialized field for a private variable.

With these changes, the script would now look like this in the Inspector:

Speed is now a serialized field that can be edited during testing in real time.

These basic details about variable declaration show how something so simple can turn into a very magical journey, or even a beneficial tool.

--

--

Alberto Garcia
Alberto Garcia

Responses (1)