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 Fix UI Input Module shown after player join

Discussion in 'Input System' started by GrayedFox, Jan 6, 2021.

  1. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    When a new player joins at the skill selection screen of my game, the player UI prefab is instantiated (as expected) but for some reason the PlayerInput component references a cloned input action, instead of the same input action as Player 1.

    I also get a warning in the editor and the controls/input device for player 1 do nothing until I hit the fix button.

    Not quite sure what's going on here?

    input-module-warn.png


    During the prefab instantiation I make sure to set the UIInputModule reference in my PlayerUIController script like so:

    Code (CSharp):
    1.  
    2.     void Awake()
    3.     {
    4.         MainMenu = GameObject.FindGameObjectWithTag("MainMenu").GetComponent<MainMenu>();
    5.         SkillMenu = MainMenu.gameObject.GetComponentInChildren<SkillMenu>(true);
    6.         UIInputModule = MainMenu.gameObject.GetComponentInChildren<InputSystemUIInputModule>(true);
    7.  
    8.         PlayerInput = GetComponent<PlayerInput>();
    9.  
    10.         PlayerInput.uiInputModule = UIInputModule;
    11.         PlayerIndex = PlayerInput.playerIndex;
    12.  
    13.         Init();
    14.     }
    Any one encountered this before?

    Edit: To clarify, the new player joins successfully and I can control the UI with the new player, but player 1's controls get ruined and player 1's inputs do nothing, until I hit the fix button.
     
    Last edited: Jan 6, 2021
  2. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    I found a workaround which at doesn't remove the warning in the editor but at least keeps all controls working.

    By caching the value of the actionsAsset being used when the first player joins, I can make sure the same asset is used for the next player(s). Put this inside your OnPlayerJoined event:

    Code (CSharp):
    1.     public void OnPlayerJoined(PlayerInput player)
    2.     {
    3.         Debug.Log("PlayerJoined");
    4.  
    5.         if (CachedActionsAsset == null)
    6.             CachedActionsAsset = GetComponentInChildren<InputSystemUIInputModule>().actionsAsset;
    7.     }
    Then, from the script attached to the prefab instantiated by the PlayerInputManager, you can do something like this:


    Code (CSharp):
    1.     void Awake()
    2.     {
    3.         MainMenu = GameObject.FindGameObjectWithTag("MainMenu").GetComponent<MainMenu>();
    4.         SkillMenu = MainMenu.gameObject.GetComponentInChildren<SkillMenu>(true);
    5.         UIInputModule = MainMenu.gameObject.GetComponentInChildren<InputSystemUIInputModule>(true);
    6.  
    7.         PlayerInput = GetComponent<PlayerInput>();
    8.  
    9.         Init();
    10.     }
    11.  
    12.     private void Init()
    13.     {
    14.         PlayerInput.uiInputModule = UIInputModule;
    15.         PlayerIndex = PlayerInput.playerIndex;
    16.         UIInputModule.actionsAsset = MainMenu.CachedActionsAsset;
    17.     }
    Assigning the cached actions asset allows the player one controls to keep working after consecutive players join. Bit of an ugly workaround but if you're stuck like I am, this band aid fix will at least allow you to test out game play and continue work.
     
    Last edited: Jan 7, 2021