Inspector

Don't worry, Chief, I'm always on duty!


Visibility of fields

Fields set as public is normally visible and possible to edit in the unity inspector. You can toggle the inspector to Debug mode at the top right of it's window to also show private variables. They cannot be modified, only viewed. Fields can be hidden from the inspector with the HideInInspector attribute. A private field can be shown modifiable in the inspector with the SerializeField attribute.

public int m_hitPoints; // Will be modifiable inspector

private int m_totalDamage; // Will only be visible in inspector debug mode.

[HideInInspector]

private bool m_resetAi = false; // Will never show up in the inspector.

[SerializeField]

private bool m_detectedByEnemy = false; // Will show in inspector and possible to change.

Renaming Serialized Fields

When you change the name of a field the old settings in prefabs will be lost but that can be solved with the FormerlySerializedAs attribute. If you change the name more then once you can add the attribute multiple times. You need to keep the attribute as long as you you have not re-saved all the scenes and assets.

[FormerlySerializedAs("m_fov")]

public float m_fieldOfView = 10;

[FormerlySerializedAs("m_fov")]

[FormerlySerializedAs("m_fieldOfView")]

public float m_fieldOfViewInDeg = 10;

How to group variables in the inspector.

Header

The HeaderAttribute adds a header text before the fields in the inspector.

[Header("Sense Settings")]

public float m_fov = 10;

public float m_viewDistance = 10;

[Header("Health Settings")]

public float m_shield = 6;

public float m_maxShield = 6;

public float m_hitpoints = 10;

public float m_maxHitpoints = 10;

Space

With the SpaceAttribute you can add a small spacing between values that are in the same group.

[Header("Health Settings")]

public float m_shield = 6;

public float m_maxShield = 6;

[Space(5)]

public float m_hitpoints = 10;

public float m_maxHitpoints = 10;

Seperate Class

You can put the variables in a seperate class, set the class as Serializable and then put a object of the class in the component.

public class Agent : MonoBehaviour

{

[System.Serializable]

public class HealthSettings

{

public float m_shield = 6;

public float m_maxShield = 6;

public float m_hitpoints = 10;

public float m_maxHitpoints = 10;

};

public HealthSettings m_healthSettings = new HealthSettings();

};

How to give hint's to designers what inspector values do

Tooltips

Using the TooltipAttribute before a value you can provide a short helpful text that show when someone try to edit the value in the inspector.

[UnityEngine.TooltipAttribute("The field of view of the agent")]

public float m_fov = 10;

Range

Use the RangeAttribute to limit the valid range of a number.

[Range(1,25)]

public float m_viewDistance = 10;