Version: Unity 6 Preview (6000.0)
Language : English
Naming scripts
Null references

Inspecting scripts

Any MonoBehaviour script can be used as a componentA functional part of a GameObject. A GameObject can contain any number of components. Unity has many built-in components, and you can create your own by writing scripts that inherit from MonoBehaviour. More info
See in Glossary
, which means:

  • You can attach the script to GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
    See in Glossary
  • You can edit the script’s properties and values in the Inspector window

The example code below declares a public field called myName. When you add this script to a GameObject in your sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
, the field becomes visible in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary
window as a field labelled My Name. The default value of none declared in the script becomes the default value in the Inspector window, which you can then change by typing into the field.

using UnityEngine;
using System.Collections;

public class MainPlayer : MonoBehaviour 
{
    public string myName = "none";
    
    // Use this for initialization
    void Start () 
    {
        Debug.Log("I am alive and my name is " + myName);
    }
}

Each GameObject you attach your script component to can have its own unique value for the field.

A public string field editable in the Inspector window.
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. However, these changes are purely for display purposes. You should always use the field name in your code.

In the Inspector window, if you edit the My Name value and press Play, the console message should now include the text that you entered.

Public and private fields

All public fields are editable in the Inspector window by default. To prevent a public variable from being displayed in the Inspector window, add the HideInInspector attribute to it. To make a private field editable in the Inspector window, add the SerializeField attribute to it.

Note: You can change the value of a script’s fields in the Editor while running in Play mode. This allows you to see the effects of changes directly without having to stop and restart. However, when you exit Play mode, the values of the fields reset to whatever they were before you entered Play mode.

Object reference fields

As well as simple built-in C# types such as bool, string, and int, you can also make any field whose type inherits from UnityEngine.Object editable in the Inspector window. This includes all built-in component types (such as Transform, AudioSource, CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
, Light), your own MonoBehaviour script types, and many asset types.

This allows you to make use of the Unity Editor’s drag-and-drop system in your own scripted components. For example, if you create a public Transform field in your script and add it to one GameObject, you can then drag another GameObject into that field in the Inspector window to set up a reference to that GameObject’s Transform componentA Transform component determines the Position, Rotation, and Scale of each object in the scene. Every GameObject has a Transform. More info
See in Glossary
, which you can then access at runtime in your script.

For example, this Follow script makes one GameObject follow another:

using UnityEngine;

public class Follow : MonoBehaviour
{
    public Transform objectToFollow;
    public float followSpeed = 1;

    void Update()
    {
        // calculate the distance between this object and the target object
        // and move a small portion of that distance each frame:

        var delta = objectToFollow.position - transform.position;
        transform.position += delta * Time.deltaTime * followSpeed;
    }
}

The script has a public field of type Transform which appears in the Editor as an assignable field. You can drag and drop a different GameObject from your Hierarchy window into this field, and the Editor assigns a reference to the Transform component attached to that dropped GameObject.

In the screenshot below, the script is placed on the Sphere GameObject, and the Cube has been dragged and dropped from the Hierarchy into the Object To Follow field.

A public Transform field with a GameObject assigned. Here the script is on the Sphere (currently selected), and the Cube was dragged and dropped from the Hierarchy into the Spheres Object To Follow field
A public Transform field with a GameObject assigned. Here the script is on the Sphere (currently selected), and the Cube was dragged and dropped from the Hierarchy into the Sphere’s Object To Follow field

Note: you can also set up default object references for public object reference fields in your MonoBehaviour scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary
.

Field name to label conversion

Unity converts C# field names to labels in the Inspector window according to a set of rules. For example, the variable names in the examples above have been converted from myName to My Name, and from objectToFollow to Object To Follow. The rules are as follows:

  • 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
  • Remove anym_ prefix
  • Remove any k prefix
  • Remove any _ prefix

There are some special cases, such as iPad or x64, where these rules are not applied.

Additional resources

Naming scripts
Null references