NullReferenceException: Object reference not set to an instance of an object
TestingInputSystem.Update () (at Assets/TestingInputSystem.cs:26)
using UnityEngine;
using UnityEngine.InputSystem;
public class TestingInputSystem : MonoBehaviour
{
private PlayerInput playerInput;
private Rigidbody2D rigidbody2DThis;
private PlayerInputActions playerInputActions;
private void Awake()
{
rigidbody2DThis = GetComponent<Rigidbody2D>();
playerInput = GetComponent<PlayerInput>();
PlayerInputActions playerInputActions = new PlayerInputActions();
playerInputActions.Enable();
playerInputActions.Gameplay.Moving.performed += Moving;
}
private void Update()
{
float input = playerInputActions.Gameplay.Moving.ReadValue<float>();
float speed = 55f;
rigidbody2DThis.AddForce(new Vector2(input, 0) * speed, ForceMode2D.Force);
}
private void Moving(InputAction.CallbackContext context)
{
Debug.Log($"Moving context = {context.ReadValue<float>()}");
float input = context.ReadValue<float>();
float speed = 55f;
rigidbody2DThis.AddForce(new Vector2(input, 0) * speed, ForceMode2D.Force);
}
}