Search Unity

"Invalid User" when enabling a gameobject with PlayerInput

Discussion in 'Input System' started by Twin-Stick, Sep 13, 2019.

  1. Twin-Stick

    Twin-Stick

    Joined:
    Mar 9, 2016
    Posts:
    111
    **EDIT**

    I decided to rethink how I am using the PlayerInput module and whacked it on a singleton which my controller then references.
    Now I think about it, it's probably the better design :)
    --------------------------------------------------------------------------------



    Hey team - hoping someone can help out..

    I have a 'player' with a PlayerInput component for it's controls, works as expected. However if I need to disable that object and re-enable it, I get an error "Argument Exception: Invalid user". This can be confirmed by monitoring the input system debugger window, when enabling the gameobject, no user is added.

    Is there a way to do this operation correctly or is it just a representation of the current state of the input system?

    Thanks!
    Nathan.
     
    Last edited: Sep 13, 2019
  2. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    I'm gonna revive this thread since I'm having the same issue you had.

    You decided to use a singleton - how and why though?

    (1) I need input for my UI actually. e.g. in a specific menu, when the player press back (escape), it should go to previous menu. That menu will be disabled then, and so the input
    (2) If I use a single object and assign the PlayerInput to it - how will I be able to use it in my menu?
     
  3. Twin-Stick

    Twin-Stick

    Joined:
    Mar 9, 2016
    Posts:
    111
    Hey there,
    I'm also using menu ui navigation this way.

    I'm simply registering the events by code and enabling the actions as required.
    There are examples on the github page but let me know if you get stuck and I'll post up my code as examples.

    Good luck!
     
  4. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    I know how to register it by code, I just find it a bit silly creating 5 different script for 5 different objects, where each one is simply doing .SetActive(false) .SetActive(true) to 2 different properties..

    I just finished creating a script where you can choose the action map and the action, and add an event, and it will register those and call the event. However, I have 2 issues. Keep in mind that when a user presses a key, the current game object is disabled, and another game object is activated. Both have the custom script I'm talking about.
    The issues are:

    (1) when registering the key in OnEnable(), it's immediately called, since the component was activated because of that key. To solve that, I can use StartCoroutine()
    (2) In my callback execution, I'm actually removing the callback. There seems to be a bug where you can do that. I just opened an issue though: https://github.com/Unity-Technologies/InputSystem/issues/942

    So currently I'm gonna stick to scripts, although I'm not a big fan of it - unless you have a better way?
     
  5. msh91

    msh91

    Joined:
    Apr 22, 2017
    Posts:
    37
    After trying to fight it some more, I ended up with simply checking the triggered property in Update().
    I believe it is less efficient, since now I have an extra component in the game loop even though there is a specific callback for it; but at least this one works.
     
  6. Twin-Stick

    Twin-Stick

    Joined:
    Mar 9, 2016
    Posts:
    111
    I have my implementation done via a state machine. So when the player state changes to something which requires a particular control mapping, the state machine calls it's own OnDisable method (which will unsubscribe from the input events) - then the new state subscribes to the required input events.

    So even if it is going from game mode to a menu UI, this implementation doesn't really care because it is only actioning those specific input events in that state anyway. I feel this is quite effective, for my needs anyway.