Learn about Unity's event order when initializing an object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class EventOrderTest : MonoBehaviour | |
{ | |
private bool firstUpdate = true; | |
private void Awake() | |
{ | |
Log("Awake"); | |
} | |
private void OnEnable() | |
{ | |
Log("OnEnable"); | |
} | |
private void Start() | |
{ | |
Log("Start"); | |
} | |
private void Update() | |
{ | |
if (firstUpdate) | |
{ | |
Log("First Update"); | |
firstUpdate = false; | |
} | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
Log("Before Spawn"); | |
gameObject.AddComponent<EventOrderTest>(); | |
Log("After Spawn"); | |
} | |
} | |
private void Log(string s) | |
{ | |
Debug.Log(gameObject.name + ": " + s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment