- Home /
two if() statements with one GetKeyDown won't work.
Hi all, I've looked but can't find the answer anywhere else, sorry if it's out there and I can't find it. All I want to do is call one of two separate coroutines with one key press, depending on the state of another boolean variable. The following works fine.....
if(Input.GetKeyDown(KeyCode.I) && follow)
{
bcOn();
}
if(Input.GetKeyDown(KeyCode.O) && boardChopper)
{
followOn();
}
however, if I change that O keystroke to an I, it doesn't work. No errors in the console, but the coroutines aren't called. It's driving me mad! I know it's coding 101 (ha, binary joke), and I'm missing something really simple, but I just can't see it.
Any help greatly appreciated
Ian
I notice you're using &&
. Sanity check time! Check if follow
and boardChopper
are true. If one of them isn't, then that might explain why you aren't getting two calls.
Thanks, but I have. Like I say, the code itself works fine with another key subbed in for one set of code, and the letter "I"'s not used elsewhere. Very strange :-/
Answer by xt-xylophone · Mar 19, 2013 at 07:48 PM
So you want to call either bcOn or boardChopper if you push the 'I' key based on the 2 booleans? Try something like this...
if(Input.GetKeyDown(KeyCode.I))
{
if(follow)
{
bcOn();
}
if(boardChopper)
{
followOn();
}
}
Not tested but if I understand you correctly, should work :D
Good call, I can see it will work. I just wish I knew why my code won't work, when it does with a different key assigned. Weird. Either way, thanks :-)