페이지

2017년 7월 19일 수요일

GameObject Instatiated Lifecycle

GameObject Instatiated Lifecycle

When you call Instantiate on a prefab, the Awake() function is run immediately, but NOT the Start() function. The Start function is run sometime later before the first call to Update().



public class Cannon : MonoBehaviour{
    public GameObject prefabBullet;
    List<GameObject> list = new List<GameObject>();
    public void ReloadBullet(){
        GameObject bullet = Instantiate( prefabBullet );
        bullet.SetActive(false);
        list.Add(bullet);
    }

    public void Fire (){
        GameObject bullet = list[0];
        bullet.SetActive(true);
    }

}



//GameObject bullet has MyBullet component.

public class MyBullet : MonoBehaviour{

    void Awake(){
        Common.Log("MyBullet:Awake");
    }

    void Start(){
        Common.Log("MyBullet:Start");
    }

    void OnEnable(){
        Common.Log("MyBullet:OnEnable");
    }

    void OnDisable(){
        Common.Log("MyBullet:OnDisable");
    }

}



Cannon c;
c.ReloadBullet();

/*
MyBullet:Awake
MyBullet:OnEnable
MyBullet:OnDisable
 */



c.Fire();
/*
 MyBullet:OnEnable
 MyBullet:Start       // MyBullet:Start() is called before MyBullet:Update() is called
 */


댓글 없음:

댓글 쓰기