Project 1: Space Shooter

Variables — What are they and why are they used?

Also, what is the difference between public and private?

The difference between private and public variables, and serialized fields.

In a way they are exactly the same as variables in mathematics or in any field of science that uses mathematical tools. Now in software engineering this has been extended from numbers, like real or integer numbers, to include other options as well, for example strings or objects among other things. In the image showing the demo script there is a variable called which is of type GameObject.

The variables are also used in a similar fashion, as place holders for longer entities that may change from time to time. That is, it is easier to write 𝛑 than a long sequence of numbers or a formula. Unlike in mathematically based systems, in software engineering the names of the variables should show the significance of that variable. As an example look at the Vector3 type variable , anyone who reads the code would understand what this variable does even if they don’t know the mechanics of its variable type.

The Player script with examples of variables and public and private type.

Another reason to have variable names comes from my last story where I implemented a simple player movement. In that implementation I used number to signify the speed of my player. Quite simple? Would I remember what it meant after a month? Or better yet, what if I want to change it? It would become tedious to find that particular piece of code and change the value, especially after the Player script gets longer and longer. On the other hand, finding all the locations where the variable is used, requires only a simple search.

The variable name starts with an underscore, , to signify that it is a private type variable. This means that it can only be changed inside this particular script and nowhere else. Except in Unity using [SerializeField] before a variable, allows that private variable to be changed inside Unity. This can be seen in the gif, where I change from positive to negative and back.

Even though I have used the [SerializeField], it will not allow the variable to be accessed from another script. This can be seen in the image below, where it shows an error message while I try to change the variable’s value. Instead public variables can be changed from another script or inside Unity as if they were serialized.

The error message when trying to access private variable from another script, even though it is serialized.

Usually though it is a bad thing to use public variables, because it could become very hard to find why the program doesn’t work as it is supposed to, if some variable’s value has been changed in some other script, since there would be no error messages. Of course there could be crashes and such if the value of the variable exceeds some limits, but even then finding what part changes the value to cause this behavior would be hard.

As an example I made a short demo script which changes the boundary value on the positive x-axis from to , when the -key is pressed. It will also show a message in the when the has been pressed just to make it more clear. For testing purposes the boundary value could be serialized, so that it can be changed in Unity, but there shouldn’t be any reason for another script to be able to change it.

The demo script where a public variable of another script is changed.

So in short, private variables can only be changed inside the script where they are introduced and public variables can be used inside the script and from another script. Thus variables should always be private type, and if there is a need to change them inside Unity use [SerializeField] instead of making them public.

--

--

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