Search Unity

Resolved SOLVED - 'Player is inactive' error on input - but it's active and working fine

Discussion in 'Input System' started by KennyTheWhale, Mar 22, 2022.

  1. KennyTheWhale

    KennyTheWhale

    Joined:
    Dec 18, 2018
    Posts:
    10
    Hi, I'll start with the context of what I'm trying to do.
    • I'm making an offline/local multiplayer battle mode for my 2D platformer.
    • Unity 2020.3.17f1, using the new input system.
    • On the player select scene, I have a PlayerConfigManager game object with a script of the same name and a player input manager with 'join players when button is pressed' ticked.
    • The first time any controller or keyboard presses something, it spawns a new 'PlayerConfiguration(clone)' object as a child of the PlayerConfigManager, with a SpawnPlayerSetupMenu script that spawns a small selection menu for that player where they can select their character colour.
    • The PlayerConfiguration(clone) also has a Player gameObject as a child, which is the object of the actual character. During the player select screen, this child Player object is set to inactive (so the object is greyed out on the hierarchy).
    • When the players select their colour and all have chosen 'ready', the in-game scene starts and the SpawnPlayerSetupMenu script uses SetActive(true) to set its own child player object as active.
    Here's where it gets weird (and more related to the input system):
    • Everything 'works' completely fine. The players all spawn and can use all their moves correctly.
    • Yet, for some reason, every time player 1 (regardless of controller/keyboard assigned) does a move e.g. jump or attack (which is meant to start a coroutine), it throws an error: Coroutine 'JumpButtonDown' (or whatever) couldn't be started because the game object 'Player' is inactive!"
    • Other players don't get this, only player 1, and it's weird because the coroutine it says can't run is definitely running - if I put print("working") inside the coroutine then that print runs. And the move gets executed as intended.
    • I'm 99% sure the Player object is active. It's no longer greyed out in the hierarchy since I set it to active, its parent/root are active, and it shows as becoming active when I use activeSelf and activeInHierarchy.
    I also think it might be input system related because the only move that doesn't throw the error is movement (i.e. walking left/right or pressing up/down to enter doors or crouch). These movements are different from the other moves as they are set up as 1D axis values with positive and negative values 1 to -1, while other moves are value action types but without an axis.

    They are also scripted a bit differently. Horizontal movement is scripted like this:
    Code (CSharp):
    1. public void OnMoveHorizontal(InputAction.CallbackContext context)
    2.     {
    3.         if (playerInputAllowed)
    4.         {
    5.             inputX = context.ReadValue<float>();
    6.             inputX = Mathf.RoundToInt(inputX);
    7.         }
    8.     }
    While other moves are scripted like this (jumping, in this case):
    Code (CSharp):
    1. public void OnJump(InputAction.CallbackContext context)
    2.     {
    3.         if (context.performed && playerInputAllowed)
    4.         {
    5.             StartCoroutine("JumpButtonDown");
    6.         }
    7.  
    8.         if (context.canceled && playerInputAllowed)
    9.         {
    10.             StartCoroutine("JumpButtonUp");
    11.         }
    12.     }
    I've tried everything I've managed to find through research for multiple hours, plus what I could think of trying myself (with my limited development knowledge - I'm better at game design). If anyone has any ideas of what's wrong or what else I can try, please let me know. Also tell me if there's anything else you'd like me to confirm. Thanks!
     
    Last edited: Mar 22, 2022
  2. luizguilherme0856

    luizguilherme0856

    Joined:
    Nov 26, 2020
    Posts:
    1
    I had a similar problem. I dont know why this happens. I think its a bug but anyway I just check if the player is not null before calling the method

    Code (CSharp):
    1.     public void OnJump(CallbackContext context)
    2.     {
    3.         if (player != null)
    4.         {
    5.              player.Jump(context);
    6.         }
    7.     }
     
    KennyTheWhale likes this.
  3. KennyTheWhale

    KennyTheWhale

    Joined:
    Dec 18, 2018
    Posts:
    10
    Thank you so much! That worked perfectly. I do wonder why the issue happens but I'm just happy it's gone