Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question New Input System : device is not paired anymore when switching control scheme

Discussion in 'Input System' started by Mathxxl, May 15, 2023.

  1. Mathxxl

    Mathxxl

    Joined:
    Jun 4, 2021
    Posts:
    2
    Hi everyone,

    I recently started using the New Input System and so far I think I understood the basics of Player Input, Action Map, Action Schemes etc.

    I'm creating a game where the player can either play using the Keyboard or a Gamepad, and I want the game to switch to the current one based on player input. I used a Control Scheme for each device because it would be useful later on to know what the player is using for tutorials for example.

    This is the code I used for switching between schemes :


    Code (CSharp):
    1.    
    2. using System;
    3. using System.Collections;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public sealed class InputManager : Singleton<InputManager>
    8.     {
    9.         [SerializeField] private PlayerInput playerInput;
    10.  
    11.         private void OnEnable()
    12.         {
    13.             InputSystem.onActionChange += OnActionChange;
    14.         }
    15.  
    16.         private void OnDisable()
    17.         {
    18.             InputSystem.onActionChange -= OnActionChange;
    19.         }
    20.  
    21.  
    22.         private void OnActionChange(object obj, InputActionChange change)
    23.         {
    24.             Debug.Log($"On Action Change : {change}");
    25.            
    26.             if (obj is not InputAction action) return;
    27.             if (change != InputActionChange.ActionPerformed) return;
    28.  
    29.             var control = action.activeControl;
    30.  
    31.             if (control == null) return;
    32.  
    33.             playerInput.SwitchCurrentControlScheme(control.device);
    34.    
    35.         }
    36. }
    However in game when I start to use the Keyboard, the Gamepad is not detected anymore. I don't know much about how to read the input debugger but it seems that once switching the device is not paired to the user anymore and therefore its input aren't registered.

    Note that I have no PlayerInputManager component in my scene and that the Gamepad is correctly detected and can be used as long as I don't switch the control scheme.

    Any idea on why this occurs and how to solve it ?
    I'm also open to suggestions if you have any other way to manage multiple devices !
     
  2. lucascavataio

    lucascavataio

    Joined:
    Nov 30, 2021
    Posts:
    1
    Hello!

    Did you find a solution?
     
  3. Mathxxl

    Mathxxl

    Joined:
    Jun 4, 2021
    Posts:
    2
    Sadly I did not.

    Furthermore since then I have moved on to another project so I haven't really touched on the subject for months haha.