1
\$\begingroup\$

So basically, when my scene in Unity changes and I'm holding down, say, Right or Left, when the scene resets, my player (and Unity) doesn't even recognize that I'm still holding that button, until I lift my finger and put it back.

Any thoughts?

Sorry for any broken English or anything, I'm running off two hours of sleep.

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

Using this in my dataHolder that persists between scenes I continuously got space key was pressed logged in the console.

 void Update () {
        if (Input.GetKey("space"))
            print("space key was pressed");
    }

The trick here is to make sure the instance of the script that handles button presses persists between scene loads.

\$\endgroup\$
1
  • \$\begingroup\$ Ah, why didn't I think of that? Thank you! \$\endgroup\$
    – user84154
    Commented Jun 2, 2016 at 14:55
1
\$\begingroup\$

I'm coming to this very late, but I ran into a similar issue. From what I tested, Unity seems to preserve keystates between scenes unless multiple keys are down, in which case it loses the first key to be pressed when a new scene is loaded. This is actually a super easy fix: Instead of using Input.GetKey(), you can use OnGUI() and use a boolean to detect if a certain key is down. Unity seems to handle this better, and the boolean will be preserved (provided that the script you're using persists). I'm not sure if this was the exact issue you were facing, but this is one way to fix it :)

\$\endgroup\$

You must log in to answer this question.