Zak The Hat Zak The Hat - 1 year ago 91
C# Question

Public variables are not visible in script Inspector window

I started Unity a couple of days ago and I'm confused.

This is my current workspace:

This is what is should look like:

I should be able to fill in the variables but I can't.

if you wish to see the scripts here is a paste bin:

Answer Source
  1. To show properties/variables in inspector, they must be public, if they are not they won't appear.

  2. You can also view private fields by attributing them as [SerializedField].

  3. To view custom classes in inspector, you should mark them as [Serializable].

  4. To hide public variables from inspector, use [HideInInspector] or [System.NonSerialized] attribute.

Here is a sample:

public class SomePerson : MonoBehaviour 
    //This field gets serialized because it is public.
    public string name = "John";

    //This field does not get serialized because it is private.
    private int age = 40;

    //This field gets serialized even though it is private
    //because it has the SerializeField attribute applied.
    private bool hasHealthPotion = true;

    // This will be displayed in inspector because the class has Serialized attribute.
    public SomeCustomClass somCustomClassInstance;

    // This will not be shown in inspector even if it is public.
    public bool hiddenBool;    

    // Same with this one.
    public int nonSerializedVariable = 5;

public class SomeCustomClass
    public string someProperty;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download