system
1
Hi All,
Is there any difference in the efficiency of code execution when using multiple if statements as opposed to making a single switch statement?
It’s on an OnTriggerEnter(hit : Collider) function to try and capture the multiple collision possibilities for a projectile. Thank you.
Cheers Chris
Joshua
2
Switch statements are indeed more efficient, but “Switch statements have extra optimization that is done in the background when compared to if-else blocks.” isn’t really the reason.
The reason is that the expression is evaluated once and then the side effects get executed. With several if else if else statements each if gets checked one by one until one returns true and gets executed.