Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Input system does not poll correcly between sceneloads

Discussion in 'Editor & General Support' started by pleasefixthis, Jun 28, 2023.

  1. pleasefixthis

    pleasefixthis

    Joined:
    Sep 14, 2015
    Posts:
    10
    I attached the following script to the main camera in an empty scene in an empty 2D project running unity 2021.3.0f1. Tl;dr held inputs do not persist between sceneloads.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class InputPresser : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         if (Input.GetKeyDown(KeyCode.R))
    9.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    10.  
    11.         if (Input.GetKey(KeyCode.RightArrow))
    12.             Debug.Log("holding right");
    13.  
    14.         //Hold down the right arrow and then press R and then P
    15.         if (Input.GetKeyDown(KeyCode.P) && !Input.GetKey(KeyCode.RightArrow))
    16.         {
    17.             //Expected behavior: the held input prior to sceneload is read correctly
    18.             //Observed behavior: the held input is not read
    19.             Debug.Break();
    20.         }
    21.     }
    22. }
    I experience the same problem using the hybrid input system. Can anyone provide a workaround?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Why as a matter of fact... I made one of these loooooong ago, see attached.

    Directions on the first scene (Scene1)
     

    Attached Files:

  3. pleasefixthis

    pleasefixthis

    Joined:
    Sep 14, 2015
    Posts:
    10
    Thanks!

    I tried using this, and I found that about 1% of the time when a scene reloads itself two instances of the same scene end up loaded at the same time with neither unloading. Once this happens it propagates and you end up with potentially hundreds of copies of the same scene in the level.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    WHOA! Seriously?! That's cool!!! I never tested it that much. Not sure what to suggest. I would probably write something to detect that condition and just dump one of the scenes perhaps??
     
  5. pleasefixthis

    pleasefixthis

    Joined:
    Sep 14, 2015
    Posts:
    10
    As far as I can tell it is a race condition, so there is no way to detect it. Unless you mean just load the scene twice and delete it, but then that causes problems with singletons running awake and using 2x the resources
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I feel like in part this is due to input not being abstracted from what's acting upon said input. I would be interested to see if input was handled independant of scenes if this issue still happens, or trying this out with the newer Input System.
     
    Kurt-Dekker likes this.