What Are Variables in Programming?

Matthew Givens
3 min readApr 17, 2021

Variables are one of the most fundamental aspects of modern programming languages. You can think of variables as a box that stores the information you need to use. Some examples include your in game health, name, or ammo. The value of a variable can be changed based on conditions in game, such as collecting a key.

When creating a variable you will need three components with the option for a fourth where appropriate. First is a public or private reference. This is how we instruct Unity on who should be able to see and modify the variable. Take your user name in a multiplayer game as an example. Everyone needs to know this information, so a public reference is appropriate. Look at the context of each situation, but in general a variables should be private unless it needs to be accessed by another script. If you neglect to declare the scope of your variable, C# will go with private by default.

The variable’s data type comes second. There are a number of data available in C#, but we will typically use the most common four. Strings store lines of text for data including names and dialogue. Booleans, or a bool, are used for simple true or false statements. Is the boss key in the players inventory? We use floats to work with decimal numbers, and integers for whole numbers. It’s important to note that the “f” suffix is required to work with float numbers.

Next you must give your variable a name in order to reference it. In any project it’s important to follow standard naming conventions for your variables. All variables should be camel case, and private variables should begin with an underscore.

Last, you may optionally assign your variable a value while initializing it. The default value for integers and bools are 0 or false respectively, but changing that could be appropriate for your context. Be sure to note the syntax for each data type. The suffix “f” for floats and quotations for strings as an example.

--

--

Matthew Givens

A self-taught Unity developer, learning more everyday and documenting my journey. My passion for the world of game development grows with every skill I pick up.