Add your own properties to the Unity editor using public variables
 
If you’ve been using unity for any amount of time you have probably used the inspector window

The inspector window lets you change the properties of all of the components of the currently selected game object. These options come in various types such as check boxes, number inputs, and spots to drag your other objects into.

But what if you want to create your own properties that are exposed to the unity editor for scripts you create?

Well the great thing is that is is really easy with the use of public variables

To start I have this cube Game Object, let’s go into the inspector and add a new script.

Let's name this script PlayerStats for example.

Now we can locate PlayerStats.cs in the Project window and double click it to open it in our code editor.

Now we can create a new variable called health, and we will make the type Int

This variable is inside the PlayerStats class so it is now usable by this class only.

If we add the public keyword at the beginning of the line to create this variable on, it tells the Unity editor, hey make a new control inside this component on this object’s inspector.

Now let’s switch back to the Unity editor and we can see because the type was Int, the unity editor knew to create a control we can type a whole number into.

We can also set a default starting value for this variable in the script by adding it here on the same line.

Now let's try another type of variable. Game Object is the variable type unity uses for game objects so let’s create one of those. Let's call it healthBar, now we can make it public and go back to the Unity editor. In the inspector you will notice what looks like a text input control, this is where you can now drag another game object to set the value of that variable to.

The next type I will show you is boolean, or true false variables.

Let’s go back to our script and add a line for a new variable of the type bool, called sleeping. Booleans in unity are set to true by default so let’s make the default false.

When we go back into the unity editor we will see the new control for sleeping and it’s a checkbox, cool

This is really useful for variables you want to be able to tweak and change, or just so you can see the status of a variable while testing out your game in the game window.

I hope this helped, and also check out the other videos in the Instant Unity series!

Subscribe for new Unity tips every week!

Help keep the series alive, become a supporter on Patreon!

Follow on twitter:
http://twitter.com/instant_unity

Subscribe to Instant Unity on YouTube:
https://www.youtube.com/channel/UCmMb8obpS9NCYtOVQDcHTZQ

Get started making your own games, download Unity here:
http://unity3d.com/unity/download/

Who is Alex?

Alex Bezuska is the cofounder/ artist for Two Scoop Games and Director at Louisville Makes Games! Alex is obsessively passionate about making games and building the local game development community. He is also an avid lover of ice cream.