Getting Started with Unity (Part 4: Variables — the building blocks of programming)

In this guide I will cover the basic types of variables, when to use them, and how they can be integrated into our Space Shooter game.

Here is where we left off. We have basic left/right player movement implemented and we are moving at 1 meter per second but we do yet have control to modify the speed as we so choose from the Unity interface.

Now, you could simply multiply a set digit between vector3.left and Time.deltaTime to create a new fixed speed. But, this will be a fixed change and could be cumbersome to come back to the code to modify each time you want to update it.

Instead, this is a perfect place to use a variable. Variables allow you to create adjustable conditions within your code, and also to be able to modify the values of those variables from within the Unity front end editor instead of your script. There are 4 main types of primitive variables: int, float, bool, and string, and a few more with more niche use cases such as long, double, and char. We will cover the basics here.

Int —stores a whole interger (non-decimal number) — Ex 5

Float — stores a fractional number (decimals allowed, note to append “f” to declare a float) — Ex 5.55555f

Bool — stores a true or false condition — Ex false

String — stores a sequence of characters surrounded by double quotes — Ex “my name is Chase”

The lions share of game logic relies on the storage and manipulation of variables. They will be critical for any project you create.

So! Back to our space shooter example. We need a variable to control speed. It makes sense to use either an int or float variable here. When I have the option I choose float because it allows for more granular flexibility. First we need to declare the variable at the top of the script, above the start method.

A few things to note here. First, every variable has a name. Here we named the variable “Speed” — a good name gives the programmer an exact idea of what that variable is for. Second, when you declare a variable you should generally give it a value, with the exception of bool which will default to false if not specified. It is also important to know that variables can be public or private. Private variables are local to the script you are working in and cannot be externally modified. Public variables can be referenced from other scripts and changed as needed. To avoid conflicting variables it is generally a best practice to default to private unless a public variable is explicitly needed. However, private variables by default are not visible in the inspector, while public variables can easily be seen for any script on a game object. To view a private variable in the inspector, we add the ever-so-useful[SerializeField] attribute:

You can now flip back to Unity and see that in the Inspector, under our Player script component, we can see and modify the value of “3” that we set in the code, but the variable is still private. Important note: regardless of what the value is in the script, if we modify it in the Unity editor, the editor value is the value that will be chosen for our game mechanics.

Now that we have declared our private variable, made it editable in the inspector, and given it a value, we can add it to our direction calculation to moderate our speed as needed.

Let’s save and see how this impacts our player controls!

Much better. It’s still a little slow for my liking so I’ve updated the speed to 7 in the inspector and it feels pretty responsive.

That’s it for now for variables — we’ll get into the other ones more as this game progresses. My next steps for this game will be to add up/down controls mapped to the W and S keys and include a player bounds. The bounds will control player behavior so that the player cannot move vertically above the halfway point and also that the player will be able to wrap on the horizontal axis to respawn at the other side of the screen (like Pac-Man!). We’ll cover that next in a short guide. See ya there!

--

--

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