Version: 2020.1
LanguageEnglish
  • C#

MonoBehaviour.Awake()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

Awake is called when the script instance is being loaded.

Awake is called either when an active GameObject that contains the script is initialized when a Scene loads, or when a previously inactive GameObject is set to active, or after a GameObject created with Object.Instantiate is initialized. Use Awake to initialize variables or states before the application starts.

Unity calls Awake only once during the lifetime of the script instance. A script's lifetime lasts until the Scene that contains it is unloaded. If the Scene is loaded again, Unity loads the script instance again, so Awake will be called again. If the Scene is loaded multiple times additively, Unity loads several script instances, so Awake will be called several times (one on each instance).
For active GameObjects placed in a Scene, Unity calls Awake after all active GameObjects in the Scene are initialized, so you can safely use methods such as GameObject.FindWithTag to query other GameObjects.
The order that Unity calls each GameObject's Awake is not deterministic. Because of this, you should not rely on one GameObject's Awake being called before or after another (for example, you should not assume that a reference set up by one GameObject's Awake will be usable in another GameObject's Awake). Instead, you should use Awake to set up references between scripts, and use Start, which is called after all Awake calls are finished, to pass any information back and forth.
Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake is called even if the script is a disabled component of an active GameObject.
Awake can not act as a coroutine.

Note: Use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor.

using UnityEngine;

public class ExampleClass : MonoBehaviour { private GameObject target;

void Awake() { target = GameObject.FindWithTag("Player"); } }

An inactive GameObject can be activated when GameObject.SetActive is called on it.


The following two example scripts Example1 and Example2 work together, and illustrate two timings when Awake() is called.
To reproduce the example, create a scene with two GameObjects Cube1 and Cube2. Assign Example1 as a script component to Cube1, and set Cube1 as inactive, by unchecking the Inspector top-left check box (Cube1 will become invisible). Assign Example2 as a script component to Cube2, and set Cube1 as its GO variable.
Enter Play mode: pressing the space key will execute code in Example2.Update that activates Cube1, and causes Example1.Awake() to be called.

using UnityEngine;

// Make sure that Cube1 is assigned this script and is inactive at the start of the game.

public class Example1 : MonoBehaviour { void Awake() { Debug.Log("Example1.Awake() was called"); }

void Start() { Debug.Log("Example1.Start() was called"); }

void Update() { if (Input.GetKeyDown("b")) { print("b key was pressed"); } } }

Example2. This causes Example1.Awake() to be called. The Space key is used to perform this:

using UnityEngine;

public class Example2 : MonoBehaviour { // Assign Cube1 to this variable GO before running the example public GameObject GO;

void Awake() { Debug.Log("Example2.Awake() was called"); }

void Start() { Debug.Log("Example2.Start() was called"); }

// track if Cube1 was already activated private bool activateGO = true;

void Update() { if (activateGO == true) { if (Input.GetKeyDown("space")) { Debug.Log("space key was pressed"); GO.SetActive(true); activateGO = false; } } } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答