Using a ButtonControl?

Hi, I have a Input Handler script thats a singleton and is on a game controller along with the player input component.

public class InputHandler : MonoBehaviour
{
public static InputHandler _instance = null;
public Vector3 moveInput;
public ButtonControl jump;
public ButtonControl attack;
private void Awake()
{
if (_instance == null)
_instance = this;
else if (_instance != this)
Destroy(gameObject);
}
public void OnMove(InputAction.CallbackContext context)
{
moveInput = new Vector3(context.ReadValue<Vector2>().x, 0, context.ReadValue<Vector2>().y);
}
public void OnJump(InputAction.CallbackContext context)
{
var control = context.control;
jump = control as ButtonControl;
}
public void OnAttack(InputAction.CallbackContext context)
{
var control = context.control;
attack = control as ButtonControl;
}
}

When I refer to a button in that singleton from another script (using InputHandler._instance.), 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!

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?

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.

But if that’s the case I would do it that way anyways, not with a single instance anyway, why not use dependency injection.

Sidenote: with the upcoming 1.1-preview.2, InputAction’s now have IsPressed, WasPressedThisFrame, WasReleasedThisFrame methods (see docs) equivalent to GetButton/GetButtonDown/GetButtonUp.

4 Likes

@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.

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.