Search Unity

PlayerInput added at runtime unresponsive until component unchecked/rechecked

Discussion in 'Editor & General Support' started by delzhand, Feb 7, 2021.

  1. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    I have a PlayerInput component I'm adding programmatically like so:

    Code (CSharp):
    1. input = gameObject.AddComponent<PlayerInput>();
    2. input.actions = Resources.Load<InputActionAsset>("controls/MenuControls");
    3. input.notificationBehavior = PlayerNotifications.InvokeCSharpEvents;
    My update function has code like so:

    Code (CSharp):
    1.         InputAction ia = input.actions["Cycle Down"];
    2.         if (ia.triggered && ia.phase == InputActionPhase.Performed) {
    3.             repeat.Set(OnCycleDown);
    4.         }
    5.         if (ia.triggered && ia.phase == InputActionPhase.Waiting) {
    6.             repeat.Clear();
    7.         }
    8.  
    The update function worked fine when I had the PlayerInput component already added manually via the editor, but when attempting to add the PlayerInput component during gameplay, it's totally unresponsive until I disable and re-enable via the inspector, at which point it works perfectly.

    Is there something else I need to set when adding the component? I've tried "input.enabled = true" and "input.ActivateInput()", neither seemed to have any effect.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Yeah, there must be. This is one of the most annoying things about editor-added stuff that sets up a ton of extra behind-the-scenes stuff that you don't get during .AddComponent calls. The entire UnityEngine.UI was / is nightmare of this type of thing, making it almost impossible to do meaningful runtime additions of UI elements without scads of extra boilerplate code.

    Since the above PlayerInput thingy appears to be in a package, you might gain some insight by checking out the source for PlayerInput, and looking if it implements such functions as Reset() or OnValidate(), which might be where the magic sauce is located.
     
  3. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    It seems like the issue wasn't with the PlayerInput component but the Actions. This is what did the trick:

    Code (CSharp):
    1.         Input.actions.Enable();
     
  4. CYYoung

    CYYoung

    Joined:
    Oct 24, 2020
    Posts:
    1
    Hi, can you provide the full codes about creating PlayerInput on runtime? I met the same issue, and the tricks above doesn't help me.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Please do not necro post to old threads. If you have a new problem, start a new post.

    When you make your new post...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    Do not TALK about code without posting it. Do NOT retype code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion. Do NOT post photographs of code.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/