Version: Unity 6 (6000.0)
Language : English
Handling events
Order of execution for event functions

Event functions

Event functions are a set of predefined callbacks that all MonoBehaviour script components can potentially receive. The callbacks are triggered by various Unity Editor and Engine events, including:

Implement the appropriate method signature in your MonoBehaviour-derived class to allow your game objects to react to the source events.

Refer to the MonoBehaviour Scripting API reference page for a full list of the available callbacks, where they are listed under Messages. The rest of this section gives an overview of some of the key groups of event functions.

Regular update events

A game is like an animation where the animation frames are generated on the fly. A key concept in games programming is making changes to position, state, and behavior of objects just before each frame is rendered. The Update function is the main place for this kind of code in Unity. Update is called before the frame is rendered and also before animations are calculated.

void Update() {
    float distance = speed * Time.deltaTime * Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * distance);
}

The physics engineA system that simulates aspects of physical systems so that objects can accelerate correctly and be affected by collisions, gravity and other forces. More info
See in Glossary
also updates in discrete time steps in a similar way to the frame rendering. A separate event function called FixedUpdate is called just before each physics update. Since the physics updates and frame updates don’t occur with the same frequency, you can get more accurate results from physics code if you place it in the FixedUpdate function rather than Update.

void FixedUpdate() {
    Vector3 force = transform.forward * driveForce * Input.GetAxis("Vertical");
    rigidbody.AddForce(force);
}

It’s also sometimes useful to make additional changes at a point after the Update and FixedUpdate functions have been called for all objects in the 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
, and after all animations have been calculated. Some examples of this scenario are:

  • When a 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
    should remain trained on a target object. The adjustment to the camera’s orientation must be made after the target object has moved.
  • When the script code should override the effect of an animation. For example, to make a character’s head look towards a target object in the scene.

The LateUpdate function can be used for these kinds of situations.

void LateUpdate() {
    Camera.main.transform.LookAt(target.transform);
}

Initialization events

It’s often useful to be able to call initialization code in advance of any updates that occur during gameplay. The Start function is called before the first frame or physics update on an object. The Awake function is called for each object in the scene at the time when the scene loads. Note that although the various objects’ Start and Awake functions are called in arbitrary order, all instances of Awake will have finished before the first Start is called. This means that code in a Start function can make use of other initializations previously carried out in the Awake phase.

GUI events

Unity has a system for rendering GUI controls over the main action in the scene and responding to clicks on these controls. This code is handled somewhat differently from the normal frame update and so it should be placed in the OnGUI function, which will be called periodically.

void OnGUI() {
    GUI.Label(labelRect, "Game Over");
}

You can also detect mouse events that occur over a GameObjectThe 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
as it appears in the scene. This can be used for targeting weapons or displaying information about the character currently under the mouse pointer. A set of event functions named with the prefix OnMouse (e.g., OnMouseOver, OnMouseDown) allow a script to react to user actions with the mouse. For example, if the mouse button is pressed while the pointer is over a particular object then an OnMouseDown function in that object’s script will be called if it exists.

Physics events

The physics engine will report collisionsA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary
against an object by calling event functions on that object’s script. The OnCollisionEnter, OnCollisionStay and OnCollisionExit functions will be called as contact is made, held and broken. The corresponding OnTriggerEnter, OnTriggerStay and OnTriggerExit functions will be called when the object’s colliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary
is configured as a Trigger (ie, a collider that simply detects when something enters it rather than reacting physically). These functions may be called several times in succession if more than one contact is detected during the physics update and so a parameter is passed to the function giving details of the collision (position, identity of the incoming object, etc).

void OnCollisionEnter(Collision collision) {
    if (collision.gameObject.tag == "Arrow") {
        ApplyDamage(10);
    }
}

Additional resources

Handling events
Order of execution for event functions
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961