When you select a script asset in the Project windowA window that shows the contents of your Assets
folder (Project tab) More info
See in Glossary, 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 displays some basic information about it, including the name of the assembly it belongs to, and a preview of the contents.
Note: Although the Inspector displays the contents of the script, you can’t edit the contents in the Inspector window.
The script Inspector also displays two buttons, Open and Execution Order.
Open performs the same function as double-clicking the script in the Project window, opening the script in the currently configured External Script Editor. You can configure which external editor Unity uses to open your 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 in the External Tools section of the Preferences window.
The Execution Order button opens the Script Execution Order section of the Project Settings window where you can configure the order in which Unity executes your 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:
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 Inspector 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.
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.
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.
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.
If you define public Object fields that can be assigned in the Editor in your MonoBehaviour script, you can set up default references for these fields. The default reference fields are visible in the inspector when you select the script asset in the Project window.
In the example above, there are three public audio clipA container for audio data in Unity. Unity supports mono, stereo and multichannel audio assets (up to eight channels). Unity can import .aif, .wav, .mp3, and .ogg audio file format, and .xm, .mod, .it, and .s3m tracker module formats. More info
See in Glossary fields, without default references assigned. You could assign audio clips to each of the AudioClip default reference fields.
If you assign default references, they’re applied when you add your MonoBehaviour as a component to a GameObject, or when you reset an existing instance of your MonoBehaviour on a GameObject to its default values.
Note: There is no ongoing link between the references on MonoBehaviour instances on GameObjects and the default references. This means if you change the default references, they’re not automatically updated on existing GameObjects.
Other types of inspector-editable fields that don’t inherit from UnityEngine.Object
(for example, public string or int fields) don’t have default fields in the Inspector. Instead, they take their default values from the script itself.
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:
m_
prefixk
prefix_
prefixThere are some special cases, such as iPad
or x64
, where these rules are not applied.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.