- Home /
More than one Update/scripts Vs Single Update. Any performance advantage?
Case 1:
MainScript.cs
Update(){...}
Script1.cs
Update(){...}
Script2.cs
Update(){...}
and more other scripts.
Case 2:
MainScipt.cs
//references of other scripts
Script1 s1;
Script2 s2;
Update(){
s1.UpdateX();
s2.UpdateX();
// other scripts...
}
Script1.cs
UpdateX(){...}
Script2.cs
UpdateX(){...}
Is there any Performance improvements if I use case 2 instead of case 1?
Update functions are called through an internal loop in the same way as your case 2.
There could be a performance benefit if you multiply identical operations, such as check a timer.
ie.
Update()
{
t += Time.deltaTime;
if (t > maxT)
{
s1.DoStuff();
s2.DoStuff();
s3.DoStuff();
}
}
... is better than doing the same in each loop if you have a lot of instances of your $$anonymous$$onoBehaviour
Update()
{
t += Time.deltaTime;
if (t > maxT)
{
// do stuff...
}
}
Answer by Dave-Carlile · Jun 17, 2015 at 12:52 PM
I would imagine Unity is optimized to loop through scripts calling Update, and you're going to end up with many Update functions anyway. Performance-wise it's not really worth the effort to consider - premature optimization and all that.
There might be an argument for rolling your own if you want to decouple your game logic from Unity's architecture.