MonoBehaviour components
Understand how to use MonoBehaviour to create script components that control the behavior of GameObjects and respond to events.
읽는 시간 4분최근 업데이트: 7일 전
The class provides the framework that allows you to attach your script to a GameObject in the Unity Editor. It also provides hooks into useful Events such as and .
MonoBehaviourStartUpdateYou can create new MonoBehaviour scripts in the Editor as described in Create scripts.
For a complete reference of every member of the class, and its technical details, refer to the MonoBehaviour script reference.
MonoBehaviourContent of a MonoBehaviour script file
Double-click a script asset in Unity to open it in a text editor. By default, Unity uses Visual Studio, but you can select any editor you like from the External Tools Preferences window. To change your default editor, navigate to Edit > Preferences > External Tools (macOS: Unity > Settings > External Tools) and change the External Script Editor.
A new script file contains the following:
MonoBehaviourusing UnityEngine;public class NewMonoBehaviourScript : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void Update() { }}
This script connects to low-level Unity Engine functionality by implementing a class derived from the built-in class called . Any -derived class can be attached to GameObjects as a component type. Each time you attach a script component to a GameObject, Unity creates a new instance of the object defined by the script. Unity names the class after the file name you supply on creation of the script asset. It's best practice to keep the class name and file name in sync. For more information about script file names, refer to Naming considerations.
MonoBehaviourMonobehaviourThe main things to note are the two functions defined inside the class:
- : Unity calls this function before gameplay begins and before calling the
Startfunction for the first time. This function is the most appropriate place for initialization work.Update - : Handles the frame update for the GameObject. This might include movement, triggering actions and responding to user input, or anything that needs to be handled over time during gameplay. To enable the
Updatefunction to do its work, it's often useful to be able to set up variables, read preferences, and make connections with other GameObjects before any game action takes place.Update
Attach a script to a GameObject
A script only defines a blueprint for a component, so none of its code will be active until an instance of the script is attached to a GameObject. You can attach a script by dragging the script asset to a GameObject in the Hierarchy window or in the window for the GameObject that's currently selected. In the Editor, you can navigate to Component > to view all the scripts in the project, including those you've created yourself. The script instance looks much like any other component in the Inspector:
Inspector
?
Scripts
?

A script component attached to a GameObject in the Inspector.
Once attached, the script runs when you enter Play mode. You can check this by adding the following code in the function:
Start// Use this for initializationvoid Start(){ Debug.Log("Hello world!");}
The command prints a message to Unity's Console window. When you press Play, the message appears at the bottom of the main Editor window and in the Console window (menu: Window > General > Console).
Debug.LogHello world!Coroutines
The class allows you to start, stop, and manage coroutines.
MonoBehaviourFor more information on coroutines, refer to Coroutines and the StartCoroutine method script reference.
Events
The class provides access to a large collection of event functions, which allow you to perform work at different phases of the game update loop and in response to game events. For a list of all event functions, refer to the MonoBehaviour script reference page.
MonoBehaviour