Say you have 500 GOs with Update and LateUpdate methods. All the Updates for all GOs run before any LateUpdate starts. That’s how Unity do.
Different game. An elephant. Its controller responds to OnInteract by instantiating a prefab, making a new GO. When are the new GO’s Awake, OnEnable, and Start called?
The elephant's controller.
public class MakeAThing : MonoBehaviour
{
[SerializeField] private GameObject thing;
private bool _isThingMade;
void Start()
{
Debug.Log($"Elephant start {Time.frameCount}");
}
private void Awake()
{
Debug.Log($"Elephant awake {Time.frameCount}");
}
private void OnEnable()
{
Debug.Log($"Elephant enable {Time.frameCount}");
}
public void OnInteract()
{
Debug.Log($"Elephant interact {Time.frameCount}");
_isThingMade = true;
Instantiate(thing, transform.position + new Vector3(2f, 0, 2f), transform.rotation);
}
void Update()
{
if (_isThingMade)
{
Debug.Log($"Elephant update {Time.frameCount}");
}
}
void LateUpdate()
{
if (_isThingMade)
{
Debug.Log($"Elephant late update {Time.frameCount}");
}
}
}
The thing made (the prefab):
public class ThingWhatWasMade : MonoBehaviour
{
private void Awake()
{
Debug.Log($"Thing awake {Time.frameCount}" );
}
private void OnEnable()
{
Debug.Log($"Thing enable {Time.frameCount}");
}
void Start()
{
Debug.Log($"Thing start {Time.frameCount}");
}
void Update()
{
Debug.Log($"Thing update {Time.frameCount}");
}
void LateUpdate()
{
Debug.Log($"Thing late update {Time.frameCount}");
}
}
The output:
Elephant awake 0
Elephant enable 0
Elephant start 1 NOTE 1
Elephant interact 2195 NOTE 2
Thing awake 2195 NOTE 3
Thing enable 2195
Elephant update 2195 NOTE 4
Thing start 2195 NOTE 5
Thing update 2195 NOTE 6
Elephant late update 2195 NOTE 7
Thing late update 2195
Elephant update 2196 NOTE 8
Thing update 2196
Elephant late update 2196
Thing late update 2196
- Elephant’s Start called in the next frame after Awake and Enable.
- Elephant interact, from the input phase, AFAIK.
- Unity breaks the usual phase sequence of doing all inputs and updates before doing anything earlier in the player loop. The newly created GO’s Awake and OnEnabled run in the same frame, immediately after instantiation. This is a good idea. I just hadn’t thought about it before.
- Back to elephant now the input is done. Update. Same frame.
- Back to Thing for Start. I didn’t expect this.
- Thing’s Update. Its special post-creation processing might be done at this point, and it takes its normal place in the Q.
- LateUpdates when expected. The thing’s LateUpdate is just another LateUpdate.
- The usual cycle from then on.
Good to know.