Using if, else if & else

Kungfuguy

Hi
This was a very frustrating lecture for me. I had the answer, or so I thought. I had both the “else if” statements in the right place…saved…went to unity/console to test…and for some reason I was able to still press up/down at the same time.
So I said well I just can’t figure this out. Went back to the course and freaked when I saw the solution. So I finished the lesson by re-entering the correct “else if” commands and saved. I saw the rest of the video and noticed the “collapse” option that was used. So I tried it. And tried to hit both up/down keys with the counter visible.
I swear, I was able to have both counters go up at the same time. Just like when I tried my solution prior to giving up and watching the solution. I am at a loss for an explanation other then I was hitting the keys .00000000001 milliseconds apart. All I know is that the counter was going up at the same time.

By the way, with this “if/else if” part of the code should you be able to hold down the up arrow and hit the down key at the same time and have the down key register?

                                                                     Thanks
                                                                        KFG
1 Like

Hello,
I don’t know if you have gone forward in the course sines you posted, but I’ve been having that same problem. Your right, by the looks of it you should be able to hold them both down and only one to give a message saying it was pressed. I did some searching for answers too, but in the ended just going ahead onto the rest of the course. A few lectures ahead we start coding it actually guessing the number and the “if/else if” thing kicked in.
So I guess maybe it has been updated to were it needs more then just a Debug.Log statement to count or work?.. which doesn’t make much since but basically if you just go on in the courses it will work itself out.

The only thing shall added though is a bit of a warning. I suspect that we are using the same version of Unity and I don’t think it like the “while” that much. We haven’t learned about it yet in the course, just when I was looking for why the “if/else if” wasn’t working I came across it. It should work to be something that can go before the first “if”. When I applied it though and clicked play in Unity it froze up. I had to reboot my computer to do anything about it. There should be a way to apply the “while” but I recommend not doing it if you come across recommendations till the lectures go over it .

Good luck!
MM

@Kungfuguy @Merry_McLaughlin part of the problem is that since the code is running in update, which runs once every frame, if your computer is fast enough itll still register both because its running 60+ frames per second, which is faster then a person can actually push the buttons. So you will sometimes have it register both, however its not registering them in the same frame. Technically if we wanted to fix this glitch we would have to run it separately from update.

3 Likes

Great…thanks for the explanation, makes sense.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms