Unity Show Variables and Classes in Inspector
Foreword
Unity has a powerful feature that helps us to modify values in the Inspector without any programming at all. This article covers everything we need to know in order to show our variables and our custom classes in the Unity Inspector.
Showing Variables
Just a Variable
Let's create a little C# script with the name "Test.cs" that has one int variable:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
int something = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Now we create a new GameObject via the top menu (GameObject->Create Empty) and add the Test.cs script to the GameObject. It now looks like this in the Inspector:
We can see that it has the Test.cs script on it, but we don't see our something variable at all.
A public Variable
Let's modify our script by adding another variable, but this time with the public prefix. In programming languages, public means that the value can be seen by other classes too. In Unity public also means that the variable is shown in the Inspector.
The modified Test.cs script:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
int something = 0;
public int i_am_public = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
After saving it, here is how it looks in the Inspector now:
Now we can easily modify the variable without opening the script again and without doing any programming.
Showing Classes
A public Class + Variable
Our Test script should have a address variable now. Assuming that we could use an address in other scripts as well, we could just create a whole class for it. Let's do it:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
int something = 0;
public int i_am_public = 0;
public Address address;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
public class Address {
public string country = "";
public string city = "";
public string street = "";
public int number = 0;
}
Now if we save and take a look at the Inspector again, here is what we see:
Even though we made the address variable public in our Test class, we still don't see it. The reason is that it's not a Unity class like "public class Test : MonoBehaviour" (each component needs to be :MonoBehaviour, but there is no reason for a simple class to be :MonoBehaviour).
Most people stop here and think that it's just not possible to show our Address in the Inspector, but it actually is.
A Class shown in the Inspector
Here is the trick: if a class should be shown in the Inspector, we just have to tell Unity to do so by writing [System.Serializable] above the class declaration.
Here is our modified script:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
int something = 0;
public int i_am_public = 0;
public Address address;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
[System.Serializable]
public class Address {
public string country = "";
public string city = "";
public string street = "";
public int number = 0;
}
Now if we save it and look at the Inspector again, this is what we get:
Done, simple as that!