Home > Mobile >  What is the difference between adding a method() as the last statement in a update and using LateUpd
What is the difference between adding a method() as the last statement in a update and using LateUpd

Time:09-26

I can't understand the use case of the LateUpdate() here. If the LateUpdate() is called after the Update(), then what is the difference the below two set of codes:

First set of code

void Update(){
    <some code>
    <further code>
    a();
}
 
void a(){
    <some code for a>
}

Second set of code

void Update(){
    <some code>
    <further code>
}
 
void LateUpdate(){
    <some code for a>
}

Are both the codes above same or is there any difference/ benefits on using the LateUpdate() instead of just calling a method as the last statement of the update()?

CodePudding user response:

LateUpdate is called after all Update functions have been called. So if you have 15 Scripts with an Update function, and one with LateUpdate -- LateUpdate in that one script won't execute until the other 15 Updates have been called. So the difference between your two examples is that in the first one, a() is run right away as part of that Update invocation. In the second, <some code for a> won't run until every script with an Update function has run.

Reference https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

  • Related