What Is A Variable?
A variable is a symbolic name or is associated as an identifier that can be changed during the execution of the program. Variables are used to hold strings, ints and decimals, to name a few, that all hold a value or a statement
Examples Of Variables
There are many types of variables in coding languages. In this case we are using c#. Some of the variables vary from:
Boolian
- Boolians have a true or false statement where a value can be identified as true or false. E.g a variable called name could have a bool that is false
Int
- Ints store a numbered value. E.g a variable called age is set to a int of 8
String
- Strings hold a text of any sort and do so by having quotation marks on either side. E.g a variable called name that is a string set to "Tom"
GameObject
- Gameobjects, specifically in Unity, have a empty input where a gameobject can be stored, thus allowing you to have control of that gameobject in the code. E.g a variable text that is a gameobject assigned to a text in unity called demotext, doing so, you have access to the properties of the gameobject text, otherwise known as demoText.
Transform
- Tranform, also specifically targeted at Unity, stores a gameObject that has a transform and has to have a transform. If the gameobject doesn't have a gameobject, Unity doesn't allow the gameobject to be dragged and dropped into the transform object. E.g you have a transform variable called box, in Unity, you have a 2D object called box that has a transform inside of it. You can drag box into the box variable that you just made and then you have access to the box transform
Float
- Floats are similar to Int but another feature that is has is that you can hold decimal numbers in it. You could also put f next to the number to say that this number is a float. E.g you have a float called height, and it is set to the height of 1.8f.
Uses Of Variables
Variables allow for developers like us to hold information in a current state, and then we can use it and, if needed, manipulate the value of it.
Some uses of variables, and the most common one is to store boolians. Boolians store true or false statements, and as a bonus can be changed throughout the code. If you have a bool called displayed and displayed is set to false, you could use this bool to assign whether or not a text has been displayed.
private bool displayed = false
public gameObect Text;
public gameObject Text2;
void Update()
{
if(Text.acitveSelf)
{
displayed = true;
}
if(!displayed)
{
Text2.SetActive(true)
}
}
In this block of code, we create a bool called displayed and set it to false. We also created two gameObjects called Text and Text2. If you don't understand what a gameObject is yet, I will cover it later on.
Next, we check if Text is active, if it is displayed, it is equal to true. Otherwise, don't do anything else.
And then, after we check if Text is active, we also check if displayed is equal to true. The only way displayed is equal to true is if Text is active.
This is a simple but effective piece of code that is used everywhere in coding. This code can be easily manipulated into a check function or a if else statement, but for this example, I will only show this code.
GameObject Variable
The gameobject variable is a special variable in Unity. This variable is used to store the gameobject you are using, using the word public in front or [SerializeField] to make it accessible to the rest of the workspace.
[SerializeField] gameObject Test
If you have a gameObject as the code above shows, saving it, you will come to an empty gameObject in Unity where anything can be put in it.
If you put anything in this slot, you can access the properties of this gameObject.
Vector Variables
Vectors are variables typically used to show axes. For example, Vector2s show x and y, while vector 3 shows x, y, and z. Vectors can be used as variables by listing Vector 2 or 3 before the variable name. In summary, vectors are used to store the axis of two-dimensional or three-dimensional objects.
Conclusion
In conclusion, this blog should of helped you understand what a variable is, the uses of a variable, different variables, and the uses of some of the variables. With these skills, you can further explore the vast world of variables, and this blog specifically targets Unity and their variable systems, but most of the stuff listed above can be used in the vast world of programming languages.