Which is more costly?

Hi there!

While doing Rick’s challenge for this lecture I did something in a different manner, but I don’t know which of these is more resource intensive.

Instead of adding this to Update():

if(!currentTarget)
{
GetComponent().SetBool(“isAttacking”, false);
}

I created a OnTriggerExit2D that does the same thing. Which of these would end up being harder on the computer in the end?

Thanks!

Rods K.

Hi Rods,

What if OnTriggerExit2D does not get executed for some reason? For example, if the enemy gets destroyed, it might be that the method does not get called. That’s something you will have to test.

Generally, if you have the Update method in your script, the Update method gets called. Checking the value of a bool variable does not have any noticeable impact on the performance because a bool is one of the most performant data types. Nowadays, our computers are so powerful that we do not do micro-micro-micro-micro-optimisation anymore.

For this reason, test your idea. If it works, keep it. If your game becomes laggy, check the Profiler.

Did this help? :slight_smile:


See also:

1 Like

Hey Nina! Hope you have been well!

This definitely does help! Although how I have a question about the scenario you presented and the functionality of OnTriggerExit2D since it has not been taught through the course (yet at least). In the case of the enemy getting destroyed, wouldn’t all of their components be destroyed as well (the one that calls the OnTriggerExit2D too)? At that point would it matter if it was not triggered. Or say if the Defender gets destroyed, the trigger will disappear, and won’t that count as a Trigger exit for the enemy?

Thank you once again!

I haven’t tested that but I’m wondering if OnTriggerExit2D gets called if the enemy’s collider is inside another collider and the enemy gets destroyed. Since the enemy’s collider does not “exit” the other collider by moving, it might be that OnTriggerExit2D does not get called in this case. As you wrote, if the enemy game object gets destroyed, all its components get destroyed along with it. A Debug.Log could help to figure out if OnTriggerExit2D gets called in this particular case.

Thank you! I will try that!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms