I’m pretty new to programming, so maybe somebody can tell me if there is a difference between
if (a==true b==true) {
//do something
};
and
if (a==true) {
if (b==true) {
//do something
};
};
Most of the times I would prefer the 1st one, but if for instance a is not true, will Unity continue to ask if b is true (and therefore use precious CPU power)?
Thanks in advance,
Thomas
EDIT: I stumbled upon another performance question. Is there a difference if I add one script to a game object compared to 2 or more scripts with the same code but split up?
For the first question, I believe both are functionally identical (at least under C++ compiler rules).
The only hazy if case is using OR. If you put functions in an if || statement you cannot guarantee that the second one will be called if the first one is true. (It’s listed as undefined behaviour).
Regardless of that, if statements are so incredibly fast that it is not a place to optimize. Do what is most comfortable because any performance gains or losses will never be measurable.
As for the second question, having 2 scripts instead of 1 is slightly slower, but unless you end up with 1000 scripts per character there will be no real measureable difference. Go with ease of development and easy to debug over performance in this situation as well.
When optimizing don’t get to stressed about the effeciency of very low level operations as 99% of the time it’s the overhead logic that’s causing it(things like are you looping 1000 times per frame through a 5000 item for loop, or are you creating and destroying mass amounts of memory each frame for temporary objects, etc…).