Still getting two logs at the same time :(

As you can see from my screenshot, i have correctly set up the else/if statement yet i am still getting to logs at the same time if i press the two buttons simultaneously… can any one help with this?

If your code is going in to an if statement it cannot go in to the else if statements that follow. They’re mutually exclusive.

I think what’s happening is you’re seeing the output of update() over multiple iterations. Don’t forget update() gets called dozens of times a second.


1 Like

I’m not sure I understand this answer. Each statement seems to have different code as far as I can see.

To further expand Phil’s answer…

As I understand it, Update() is executing over and over and over again automatically while the program is running. Since the game has very little else to do at this point, Update() is probably running 10’s of 1000’s times per second.

It’s incredibly unlikely that you can perfectly time the pressing of the two keys to register at the exact same moment in time to the nano-second. More likely one keypress is a few ms’s slower than the other.

So update() is picking up the first keypress straight away and sending it’s statement to the log. Then in the next heart-beat, Update() as run again and picked up the second keypress a nano-second or so later - that you thought you pushed simultaneously.

Thus, you get 2 log entries when you press 2 keys.


From my experience in coding, generally one would put in a brief time delay function at the end of the upload function if it created a conflict in the software experience. All that is occurring is the processor is really reading the code at a super fast speed, and looping back through before you can blink an eye.

This may be jumping ahead in the course… but I just used a “switch” statement to handle the keypresses… I don’t think it will stop the handling of nearly simultaneous keypresses, but it makes the coding more streamlined and will actually run a bit faster than multiple if/thens. This may very well be handled in a subsequent lecture video, but the solution is to create an event handler for the keypressed event and put the code in there.

In any case, here’s the switch statement for those that are curious:

	case KeyCode.UpArrow:
		print ("UpArrow pressed.");
	case KeyCode.DownArrow:
		print ("DownArrow pressed.");
	case KeyCode.Return:
	case KeyCode.KeypadEnter:
		print ("I WON!!!");

Apparently, the text editor here doesn’t like tabs, so I used a “/” for each indention mark. I’ll figure out how to make it purdy later. It’s past 12:30 here…

EDIT: Ok, so the compiler was kind enough to remind me that switch can only be used for certain types, and cannot be used for differentiating between different KeyCode types… And with this, I go to bed… though I’ll leave the switch statement up there for the sake of showing proper syntax, for those that would like it… Goodnight…

It’s actually two things;

  • the use of the spoiler feature initially
  • not using the preformatted text feature

I’ve removed the spoiler, and preformatted the tabbed code block for you :slight_smile:

Privacy & Terms