Search Unity

Using a ButtonControl?

Discussion in 'Input System' started by djc51401, Oct 12, 2020.

  1. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    Hi, I have a Input Handler script thats a singleton and is on a game controller along with the player input component.

    Code (CSharp):
    1. public class InputHandler : MonoBehaviour
    2. {
    3. public static InputHandler _instance = null;
    4. public Vector3 moveInput;
    5. public ButtonControl jump;
    6. public ButtonControl attack;
    7. private void Awake()
    8. {
    9. if (_instance == null)
    10. _instance = this;
    11. else if (_instance != this)
    12. Destroy(gameObject);
    13. }
    14. public void OnMove(InputAction.CallbackContext context)
    15. {
    16. moveInput = new Vector3(context.ReadValue<Vector2>().x, 0, context.ReadValue<Vector2>().y);
    17. }
    18. public void OnJump(InputAction.CallbackContext context)
    19. {
    20. var control = context.control;
    21. jump = control as ButtonControl;
    22. }
    23. public void OnAttack(InputAction.CallbackContext context)
    24. {
    25. var control = context.control;
    26. attack = control as ButtonControl;
    27. }
    28. }
    When I refer to a button in that singleton from another script (using InputHandler._instance.<ButtonControl>), I always get a null reference error. Im trying to use a ButtonControl because if I just use (context.performed) it doesnt act like the old "Input.GetButton / Input.GetButtonDown". With the button control i can use wasPressedThisFrame & isPressed I cant seem to figure out what's causing it!! Thanks for the help!
     
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I'm not sure why you would need InputHandler to be a single instance in the first place.

    but are Jump/Attack been filled out, how do you want the button to act?
     
  3. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    Because i don’t want input to be handled twice or in multiple scripts. and like i said, i’m trying to get functionality like the old “input.getkey/getkeydown” and using context.performed/started doesn’t work the same. i use a button control so i can use button.waspressedthisframe/ispressed which is like the old input.
     
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    But if that's the case I would do it that way anyways, not with a single instance anyway, why not use dependency injection.
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Sidenote: with the upcoming 1.1-preview.2, InputAction's now have
    IsPressed
    ,
    WasPressedThisFrame
    ,
    WasReleasedThisFrame
    methods (see docs) equivalent to GetButton/GetButtonDown/GetButtonUp.
     
  6. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Rene-Damm

    Cheers for the info, although I did find out one problem on android that if your checking for a joystick e.g. UnityEngine.InputSystem.Joystick it will report back that it is present, even if you don't have one.
     
  7. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Sounds like either a device having that flag on or Android adding a virtual device. Would you mind filing a ticket with the Unity bug reporter for this one? Should get looked at.