There is such a piece of code

while (key == KeyCode.None) { foreach (ControllerPollingInfo info in ReInput.controllers.Keyboard.PollForAllKeys()) { key = info.keyboardKey; } } 

Suspends when launching Unity to death. He collects data on keystrokes, and, apparently, does it too quickly.

The obvious solutions that occurred to me -

1) to put this piece of code into a separate method and call it in Update - it should seem to hang less. 2) Add cortina with, say,

 yield return new WaitFoSeconds 0.25f, 

4 polls per second should be enough for the correct polling of the keyboard and should not be heavily suspended.

Does anyone have a simpler and more elegant solution?

  • one
    your piece is too strange ..... inside the loop, you also make a loop, in which the key variable is always assigned only the last value from the collection. Nafig this cycle is another - it is not clear. What's inside ReInput.controllers.Keyboard.PollForAllKeys() is not clear. What do you want to achieve with this is not clear. What is there outside this piece of code (maybe the problem is not exactly in this piece) - it is not clear - Alexey Shimansky
  • one
    @Dmitrii do not need to alter. Tell me why you bypass all the values ​​in the loop, if only the last is in the key ? What's the point? - Alexey Shimansky
  • one
    @Dmitrii I have suspicions that the code of your program is leaving in an infinite loop ... therefore it hangs tightly. Most likely the problem is that key is never equal to KeyCode.None. Try to push off from this. Add a key to the console inside the foreach, and see if your code works the way you want it. - Oleg Klezovich
  • 2
    Of course the veil. You have started the idle cycle. - VladD
  • one
    Push in instead of KeyCone.None !Input.anyKey condition is true if no key is pressed. - JetFly

1 answer 1

Take away while. If you need to check the key - do it after or inside foreach