I’ve currently written some functionality into my game such that when the user goes in a new area, a new level is loaded asynchronously that puts the user in the same relative position so that the level load appears seamless. The one issue I’ve been having is that if a key is held down before a level loads, when the new scene actually loads, neither a getkeydown or getkeyup call comes from the key that was being held down. I’ve researched the issue and it seems this is a legitimate bug in Unity?
Has anyone come across an obscure solution to this problem? It seems a frustrating solution would be to put everything from a scene into a prefab and then simply instantiate the prefab? The only issue with this solution is I don’t believe its possible to make prefabs from light probes – which my scenes have…
Let me know if you’ve encountered this issue and/or have found a solution!
To be clear, do you mean that if it was held down, there won’t be a GetKeyUp event for when the key is released the first time it’s released, or do you mean that all future events with that button won’t trigger because the state was wrong when the scene started?
Either way, have you tried destroying and recreating the EventSystem? Or see if the same problem exists with a button as a key, by creating a new single-key button? Your prefab idea is… interesting, but it’s definitely overkill (times a hundred). There is a solution to your problem that won’t involve cutting the entire scene system out of the game, I guarantee it.
So they have the key pressed down in one scene & are still holding it down when the new scene is loaded but then it doesn’t register when the key is let go & goes up?
I’m assuming there is something like a bool in the background that says when it is pressed so get key down is when it’s true & get key up is when it is let go & the bool changes? If so then every key would likely start as up when a scene loads.
@tedthebug Unless the EventSystem is set to DontDestroyOnLoad, in which case who knows? I don’t know enough about the internal workings of Unity’s input system to really give a clear answer on this, but the function I linked to in my edited reply should fix it. It would be interesting to test in what situations this problem occurs, though.
I found a bug report that claims it happens when FocusControl() is used when a button is held down, and also for minimizing the game with a button held. I haven’t heard these problems before, but the issue still says “Active”…
Sorry for the ambiguity, what I meant was what tedthebug said – the key is not registered the first time it is released after a scene is loaded. I will try the resetinputaxis function, I assume I call it on scene load?
Edit: Just tried calling the function on both scene load and scene unload, but neither call fixed the issue. I’m still not getting any new calls until the key is released and pressed again.
Yeah, all of the input is reset on loading a new scene. I believe it’s by design, rather then a bug. But there are a few corner cases like yours where it causes problems.
I’m not sure there is an effective way around it. Does GetKey or GetAxisRaw return the correct result?
I assumed the wrong case then, so yeah, my solutions wouldn’t work for that. I suggest that you use GetKey for the inputs you’re wanting to carry across level loads and use bools to keep track of the up/down states yourself in an object/script that’s set to DontDestroyOnLoad.
You could also look around for wrappers or replacements for the Input class that would probably automate this process for you. I haven’t had to deal with this yet (I develop for Android), but I’m guessing there’s probably half a dozen or more free and commercial assets for this- probably called “Advanced Input” or “Input Class Wrapper” or things like that. Some of them might not help with this problem though, so I’d be careful and check the documentation (“alt-tab safe” or “minimize safe” or some variation might be listed as features) or actually ask the developers.
That and additive loading (your original idea) are really your only options, as far as I can tell…
Let me try getKey, hopefully that will work. If not, I think I’ll just make the user press the key again… Was hoping for seamlessness, but alas.
Edit: Yep, just what I expected, using get key on the key that was pressed returns false after level load. Kinda frustrated by this. First time I have truly come up against and unsolvable problem.
If you are holding down a key and reload the scene that key will remain held down UNLESS you pressed any other key or button.
Try it out by holding down a key then reload the scene via a timer or something. Unity will remember that the key is held down. Now do it again but this time keep the key held down then press another key. Now Unity has forgotten the previous key that is still held down only after the scene loads.