Performance Update function

Hi all

I have two short questions

  1. What function ,in terms of performance, it takes fewer resources
    Update() or OnCollision Enter () / Stay () / Exit ()?
    I mean is it better to use OnCollision …if possible or there is any difference?

  2. I’ve seen in a number of projects that one object had a few scripts, and each script had Update() function.
    How does it look from the point of efficiency?
    Is it better to try use one Update () function per object (if possible), or there is no difference?

Regards
Mirek

2 Answers

2
  1. This is fairly situation-dependent. On a purely technical level, Update is more expensive as it runs as many times as possible, whereas collision based functions are only called when certain collision-based situations occur. In my opinion the best way on a design standpoint is to have the collision functions change the state of the object, and then Update/LateUpdate/FixedUpdate reacts according to the change of state. This reduces code clutter, divides responsibilities, and is essentially more readable.

  2. This one is also very situational. If you have a need for all 3 to act/react independently of each other (even if they are related, their actions are just reactions of others) and it is coded efficiently, I’d say the differences would be negligible. You could do it all in 1 update call if you don’t need all 3 to have that independence, but you may have an increase of memory usage since you’d likely have to store references to those other GameObjects, and possible a slightly higher load time if you don’t have the objects set from the Inspector and have to use a Find function.

General Notes -
The Update and Collision calls, if I’m not mistaken, don’t actually exist in MonoBehavior scripts. Unity is using the SendMessage function on each active/enabled (I forget which, nor if these are different from one another) GameObject, and it’s just looking for functions called Update/OnCollision[whatever] whenever the time comes for that function. This is why in the performance guide they recommend disabling GameObjects if you’re Update call isn’t used, as each call is doing nothing and it wasted time finding the Components that had that function.

Let me know if that makes sense, and if you would like any further explanation or elaboration. :slight_smile:

If there is nothing being called inside a function it’s cost will be meaningless. Even if your script has no OnCollisionEnter method I believe it will be called anyway when the collision happens. When you implement it you are just declaring that you are now accepting the collision callback events in the object, which happens anyway.

Yeah, thats what I think, Unity just calls the function on all objects, if have implemented something, then something will be done, otherwise the call is wasted. Post your comment as a new answer so the OP can review it and set it as an answer if he wants.

–

OK if I well figured 1) Update function is called 60 times per second and 60 times per second is run the code in this function. However, a OnCollision.. functions is also called 60 times per second, but the content of functions is performed only at the time of the collision. If this is true definitely better to use OnCollision.. functions 2) no matter how many scripts with Update function has the object as long as each one is well programmed for efficiency

–

Update is called as fast as possible, he is not frame dependent. For a depth explanation of Update and Fixed Update please take a look here : [http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html][1] [1]: http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html

–

There is a cost to Update, even if nothing is inside it. So don't leave empty Update function inside scripts that aren't using Update.

–