- Home /
switching booleans
i have this code
var ispaused : boolean = false; function Update () { if(Input.GetKeyDown("p")){ Switch(ispaused); }
}
function Switch (bool){ if(bool == false){ bool = true; } else{ bool = false; } return bool; }
the code is supposed to switch the var ispaused, but it does not. Any help on the matter would be very nice.
Answer by Cyclops · Jun 03, 2010 at 07:34 PM
I'm not that familiar with Javascript, is it passing the Boolean by reference, or value? If it's by value, then you need to say:
ispaused = Switch(ispaused);
Answer by Eric5h5 · Jun 03, 2010 at 07:59 PM
You can actually remove the function entirely, and use this instead:
if(Input.GetKeyDown("p")){
isPaused = !isPaused;
}
@Cyclops: well, you actually answered the question. ;) I didn't really answer the question, I proposed something entirely different (albeit more efficient)...the correct answer is useful even if the particular function in question isn't in this case.
@Eric5h5: so - I gave an Answer that was technically correct, yet still managed to be inferior to an Answer that didn't even address the Question. Thanks, I feel better now... :)
@Cyclops: Nah...if 3dDude runs into a similar problem with a completely different function, he'll know what to do because of your answer, so it's not inferior. ;)
Your answer
Follow this Question
Related Questions
If...else statement with errors 1 Answer
Camera switch after specific time 1 Answer
Weapon pick up and switching script 2 Answers
Initialize multidimensional boolean array as all false Unity JS 2 Answers
Change Variable on Another Script 2 Answers