Hi guys and welcome to a basics tutorial all about public and private variables.

We will look at the difference between the two and make some changes to the MyScript we are currently working on in this project.

Private variables can only be accessed by the class they are in. They are usually used for specific jobs contained in the class. All our variables in our script are currently private. You can be implicit and add the word private in front of the Type when the variable is declared but it isn’t necessary.

Private Variables

Public variables can be accessed by other classes in the same assembly. The assembly in our case is the Unity project. They are usually used for jobs other scripts and objects could require access to. For example the players score will quite often be public so multiple systems can access it. It is usually best practice to have variables as private first and then make them public when required.

Unity allows public variables to be changed in the inspector. This can be handy for things which we as developers may want to experiment with before settling on a final value or for custom engines as it allows changes without having to look through code and find the correct place to change the value.

Note: It is possible to do this with private variables to, however it requires making changes to the editor which is a bit more advanced and won’t be covered in this course.

With this in mind lets make a few of our variables public. To do this add public before the type of the variable when it is declared. Let’s make playerName and playerSpeed public.

Public Variables

To make your code easier to read it is often best to group public and private variables separately with a line space.

Variables Sorted

Save the script and return to Unity. If you select the player object in the hierarchy, under the MyScript component you should be able to see the two variables we just made public.

Variables in the Inspector

There is an issue however. Try changing the values in the inspector.

Changed the Values

When you press play the values change back to what we set them as in the code.

Change Back

This is because setting them in the start method overrides what we entered in the inspector.  To fix this we simply delete the lines which set our two variables in the Start method.

Start Method

We should probably put in some default values to our public variables so that if a name or health is not entered the game will still have some values. To do this we will set the value of the variables when we declare them. If we set them here anything in the inspector will override them but whenever we attach a MyScript to an object there will be default values already there.

Setting Default Values

Now try removing and re attaching MyScript to the player object. To do this click on the little gear icon at the top right of the MyScript component and click remove component. Then re-attach it by dragging it from the project folder to the Player object’s inspector. The values for playerName and playerSpeed should be the default values.

Removing a Component
Default Values

Note: You do not need to remove a component to change the values in the inspector back to the default you can simply press the reset option after clicking on the cog. I just wanted to show you how to remove a component.

So that covers public an private variables for now. In the next part we will import a model to unity to act as our space ship.