【只怕没有几我的能说清楚】系列之一:Awake/Start区别

1. Awake方法在脚本对象实例化时执行对象

2. Start是脚本有效时执行
 
若是在脚本对象实例化后,直接访问脚本对象的属性(初始化后的属性),则属性的初始化须要写在Awake方法
using UnityEngine;
using System.Collections;

public class TestAwakeStart : MonoBehaviour
{
    void Start()
    {
        Create();
    }

    void Create()
    {
        //1. Awake在 AddComponent 后执行
        //2. Awake执行完后,再执行 AddComponent 的下一行
        //3. Start在 Create 后执行
        GameObject go = new GameObject("go");
        Test test = go.AddComponent<Test>();
        Debug.Log("AddComponent Next Line");
    }
}
 
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("Awake");
    }
    void Start()
    {
        Debug.Log("Start");
    }
}

打印结果:blog

相关文章
相关标签/搜索