Awake & Start

MonoBehaviour.Awake()

  Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

  Awake is called when the script object is initialised, regardless of whether or not the script is enabled.

  可以将Awake当作构造函数来使用。

1 using UnityEngine;
2 using System.Collections;
3 
4 public class Example : MonoBehaviour {
5     private GameObject target;
6     void Awake() {
7         target = GameObject.FindWithTag("Player");
8     }
9 }
View Code

相关文章: