Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active April 28, 2019 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlaShG/1f57acefc3a0834882e0fe3a42ac1d1a to your computer and use it in GitHub Desktop.
Save FlaShG/1f57acefc3a0834882e0fe3a42ac1d1a to your computer and use it in GitHub Desktop.
Learn about Unity's event order when initializing an object.
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