How To Use Variables In C#

First off we have to understand what a variable is.

A variable is a storage location that you can use to declare a data type and assign that data type a corresponding value. This means that if you plan to use a reference to something repeatedly, you can just name that value and declare it as a variable to be called upon throughout your script(s).

So what makes up a variable?

Variables consist of 4 parts: The Reference, Data Type, Name, and Value. The fourth one (Value), doesn’t have to be declared in your script and is thus “optional”. Every variable has a default value though.

[Definitions]

“Public” variables are visible to other scripts and “Private” variables are not.

“Int” stands for “Integer” and represents a whole number.

“Float” represents a number that may contain a decimal value.

“Bool” stands for “Boolean” and is a true or false value.

“String” is a line of text or (a string of characters).

[ADVANCED TIP]

It is not good practice to directly change variables from other scripts as it can bog down your program. To do this properly you should call a separate method from within the script that contains the variable and change it in there.

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store