Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bug Controls Changed Event not invoking on scene change

Discussion in 'Input System' started by WarmedxMints, May 8, 2020.

  1. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    968
    Hello, I am not sure if this is a bug or if I am doing something wrong. However, as stated in the title, when I change scenes the ControlsChangedEvent from the PlayerInput component is not firing. I have tried using SendMessage and C# events with the same result.

    My player index remains the same between scenes as it is a single-player game and the Device Lost and Device Regained events fire when they should. There is also no issue with my inputs being read.

    As the project is quite large, to eliminate anything else in the project causing issues, I made a new project with one script to read two inputs and add a listener to the unity event controlsChangedEvent.

    When loading the first scene, all is good. Once you change scenes, the inputs work but the event does not invoke.

    Here is the simple script I threw together. All I did was put that on a gameobject with the PlayerInput Component and duplicated the scene changing the buildIndex int on the second scene so I could switch back and forth while testing. I have also attached a zipped file containing the test project.

    Thanks

    Code (CSharp):
    1. public class InputTest : MonoBehaviour
    2. {
    3.     public PlayerInput playerInput;
    4.  
    5.     public InputActionAsset InputAsset;
    6.     private InputActionMap _map;
    7.  
    8.     public int buildIndex;
    9.        
    10.     private void Start()
    11.     {
    12.         playerInput.controlsChangedEvent.AddListener(test);
    13.        
    14.         _map = InputAsset.FindActionMap("Map");
    15.         _map.Enable();
    16.  
    17.         _map["Space"].performed += OnPress;
    18.         _map["Scene"].performed += OnSceneChange;
    19.     }
    20.  
    21.     private void OnDisable()
    22.     {
    23.         playerInput.controlsChangedEvent.RemoveListener(test);
    24.  
    25.         _map["Space"].performed -= OnPress;
    26.         _map["Scene"].performed -= OnSceneChange;
    27.     }
    28.  
    29.     private void OnSceneChange(InputAction.CallbackContext obj)
    30.     {
    31.         UnityEngine.SceneManagement.SceneManager.LoadScene(buildIndex);
    32.     }
    33.  
    34.     private void OnPress(InputAction.CallbackContext obj)
    35.     {
    36.         Debug.Log("Pressed");
    37.     }
    38.  
    39.     private void test(PlayerInput arg0)
    40.     {
    41.         Debug.Log("Controls Changed");
    42.     }
    43. }
     

    Attached Files:

    jkolowca likes this.
  2. ArmandTutu

    ArmandTutu

    Joined:
    Aug 7, 2014
    Posts:
    2
    Hello,

    To solve this problem, you just need to set "Behavior" parameter of your PlayerInput on "Invoke Unity Events" !

    :)
     

    Attached Files:

  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    968
    Sorry but no, that's not the issue. The prefab which the playerinput is on in every scene is set to invoke unity events
     
  4. GaZnoDrone

    GaZnoDrone

    Joined:
    Mar 3, 2015
    Posts:
    28
    I was able to reproduce it, does seem like a bud. I was registering my method on enable and deregistering on disable.
    I tried using the same Player in my initial scene throughout the scene by making it DoNotDestroy and tried it out. It worked that way.
    Is it possible for you to change it to such implementation at this time?
     
    Liderangel likes this.
  5. Liderangel

    Liderangel

    Joined:
    Jul 8, 2018
    Posts:
    13
    Same here, I could reproduce it on scene change. Even if you unsubscribe from the event OnDestroy or a C# Destructor it still won't get called.
    In the meantime I solved it using GaZnoDrone solution. But it is a bug nonetheless.
     
unityunity