Scripts in the Inspector window
Understand how the Inspector window displays C# script assets and the fields of script components.
Read time 6 minutesLast updated 5 days ago
You can access fields from your script and assign values to them in the window. You can change the value of a script's fields in the Editor while running in Play mode. This shows the effects of changes directly without having to stop and restart. However, when you exit Play mode, the values of the fields reset to what they were before you entered Play mode.
Inspector
?
Script assets in the Inspector window
When you select a script asset in the Project window, the Inspector window displays some basic information. You can view the name of the script assembly and a preview of the contents of the script.

The script Inspector displaying an example script.
The script Inspector window also displays two buttons:
Button | Description |
|---|---|
| Open | Same as double-clicking the script in the Project window, which opens the script in the currently configured external script editor. You can configure which external editor Unity uses to open your scripts in the External Tools section of the Preferences window. |
| Execution Order | Opens the Script Execution Order section of the Project Settings window where you can configure the order in which Unity executes your scripts. |
Script components in the Inspector window
You can use any script as a component, which means you can:
MonoBehaviour- Attach the script to GameObjects.
- Edit the script's properties and values in the Inspector window.
The following example code declares a public field called . When you add this script to a GameObject in your scene, the field becomes visible in the Inspector window as a field labeled My Name. The default value of declared in the script becomes the default value in the Inspector window, which you can change by typing into the field.
myNamenoneusing UnityEngine;public class MainPlayer : MonoBehaviour{ public string myName = "none"; void Start() { Debug.Log("I am alive and my name is " + myName); }}
Each GameObject you attach your script component to can have a distinct value for the field.

A public string field editable in the Inspector window.
Field names are converted to Inspector window labels according to the rules described in Field name to label conversion. These changes affect only the label displayed in the Inspector window. In your code, always use the field name.
If you edit the My Name value in the Inspector window and then enter Play mode, the message in the Console window includes the text that you entered.

A debug message appears in the Unity console, which reads "I am alive and my name is Earl".
Public and private fields
All fields are editable in the Inspector window by default. To prevent a public field from being displayed in the Inspector window, add the attribute to it. To make a field editable in the Inspector window, add the attribute to it.
publicHideInInspectorprivateSerializeFieldObject reference fields
In addition to simple types such as , , and , you can make any field whose type inherits from editable in the Inspector window. This includes all built-in component types (such as Transform, AudioSource, Camera, Light), your own script types, and many asset types. For information about which field types Unity can serialize and display in the Inspector window, refer to Serialization rules.
boolstringintUnityEngine.ObjectMonoBehaviourYou can drag objects into fields of script components in the Inspector window to assign references to objects. For example, if you create a public field in your script and add the script to one GameObject, you can drag another GameObject into that field from the Inspector window. You can then access the GameObject at runtime in your script.
TransformThe following script makes one GameObject follow another:
Followusing UnityEngine;public class Follow : MonoBehaviour{ public Transform objectToFollow; public float followSpeed = 1; void Update() { // Calculate the distance to the target object and move a small portion of it each frame. var delta = objectToFollow.position - transform.position; transform.position += delta * Time.deltaTime * followSpeed; }}
The script has a public field of type which appears in the Editor as an assignable field. You can drag a different GameObject from the Hierarchy window into this field, and the Editor assigns a reference to the Transform component attached to that GameObject.
TransformIn the following screenshot, the script is on the GameObject, and the GameObject is assigned to the Object To Follow field through the Inspector window.
SphereCube
The Sphere GameObject has a public field that refers to the Transform of the Cube GameObject.
Default object references
If you define public Object fields that can be assigned in the Editor in your script, you can set up default references for these fields. The default reference fields are visible in the Inspector window when you select the script asset in the Project window.
MonoBehaviour
A MonoBehaviour script with three AudioClip fields. The default references for these fields are shown unset.
In the previous example, there are three public fields without default references assigned. You can assign an to each of these default reference fields.
AudioClipAudioClipIf you assign default references, Unity applies them when you add your as a component to a GameObject, or when you reset an existing instance of your on a GameObject to its default values.
MonoBehaviourMonoBehaviourOther types of fields that are editable in the Inspector window and that don't inherit from (for example, public or fields) don't have default reference fields in the Inspector window. Instead, they take their default values from the script itself.
UnityEngine.ObjectstringintUnassigned reference exceptions
If your code uses a member of a field that's not assigned in the Inspector window, Unity throws an exception. In the Editor, Unity throws an with a message that names the field and the script that contains it. In a built player, Unity throws a .
UnityEngine.ObjectUnassignedReferenceExceptionNullReferenceExceptionUnassignedReferenceExceptionNullReferenceExceptioncatch (NullReferenceException)nullusing UnityEngine;public class LightColorSetter : MonoBehaviour{ // Assign this field in the Inspector window. public Light myLight; void Start() { if (myLight == null) { Debug.Log("myLight isn't set in the Inspector window."); return; } myLight.color = Color.yellow; }}
In this example, is a field that you need to set in the Inspector window. If you don't set it, the check is , and the code logs a message that reminds you to set the field instead of throwing an exception.
myLightLightnulltrueField name to label conversion
Unity converts C# field names to labels in the Inspector window according to a set of rules. For example, Unity converts the field names in the previous examples from to My Name, and from to Object To Follow. The rules are as follows:
myNameobjectToFollow- Remove an or
m_prefix._ - Remove a prefix if an uppercase letter follows it, for example
k.kMaxSpeed - Capitalize the first letter.
- Add a space between lowercase and uppercase characters.
- Add a space between an acronym and an uppercase character at the beginning of the next word.
Unity doesn't apply these rules to some special cases, such as or .
iPadx64