I'm having this script, enabling or disabling a component in Update() depending on a bool.
I guess it would be better to just set it once each time the boolean changes, but as I’m still very new to this and time is running short, this appeared to be the easiest solution…
function Update ()
{
if ( !ActivePlayerControll.waschActive)
{
light.enabled = true;
}
else
{
light.enabled = false;
}
}
Question is, since this would be applied to quite a few GOs, is it any draining on the system? (Can't really imagine, but better save than sorry...)
As you said, it would be better to just change it once. It's probably not a HUGE drain, but there shouldn't be any reason to constantly poll/set stuff in Update. I don't know what you're doing exactly, of course, but something along these lines might give you an idea:
// Disable lights in all objects tagged "Foo"
var lightObjects = GameObject.FindGameObjectsWithTag("Foo");
for (object in lightObjects)
object.light.enabled = false;
That could still be optimized further, such as by only doing the Find once and keeping the result, and then only re-doing it if the number of objects tagged "Foo" ever changed.
Thank you, Eric, for your answer =) ... works like a charm =) I thought of these logics, but would have probably taken me hours to figure out the syntax. ^^' -.- First steps are so embarassing :p
Not sure how much it costs to make setting if it does not change it it from previous state but youc ould use this code if you want to be safe.
function Update ()
{
if (( !ActivePlayerControll.waschActive)&(light.enabled== false))
{
light.enabled = true;
}
if (( ActivePlayerControll.waschActive)&(light.enabled== true))
{
light.enabled = false;
}
}
Thank you for your answer =) ... Well, d'oh! I should have been able to think of this myself ^^' blushes ... As a side-question... is there a difference between ( (...) & (...) ) and (... && ...) ?
& is a bitwise AND operator, && is a logical "and". In this particular case, when used with booleans, they get the same result (in other cases, they won't). Since it's performing a simple math operation instead of two comparisons, it's slightly faster. The parentheses are just there for visual clarity and aren't actually necessary. You should generally stick with && unless you know bitwise math.
Thank you, Eric, for your answer =) ... works like a charm =) I thought of these logics, but would have probably taken me hours to figure out the syntax. ^^' -.- First steps are so embarassing :p
– SisterKy