What am I trying to create => I want to add shoot ability to my player via New Input System
This is the New Input System Script
This is My Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[Header("References")]
[SerializeField] float speed;
[SerializeField] float range;
[SerializeField] GameObject Gun;
[SerializeField] Camera cam;
[Header("Component References")]
Animator animator_C;
SpriteRenderer spriteRenderer_C;
Rigidbody2D rb2D_C;
BoxCollider2D boxCollider2D_C;
PlayerInput playerInput; // a class not a component
private void Awake()
{
animator_C = GetComponent<Animator>();
playerInput = new PlayerInput();
spriteRenderer_C = GetComponent<SpriteRenderer>();
rb2D_C = GetComponent<Rigidbody2D>();
boxCollider2D_C = GetComponent<BoxCollider2D>();
}
private void Start()
{
// Enable player input first
playerInput.Player.Enable();
}
#region Subscribe/Unsubscribe to Event and Methods
void OnEnable()
{
playerInput.Player.Hit.performed += OnHit;
playerInput.Player.Hit.canceled += OnUnHit;
playerInput.Player.Shoot.performed += OnShoot;
}
private void OnDisable()
{
playerInput.Player.Hit.performed -= OnHit;
playerInput.Player.Hit.canceled -= OnUnHit;
playerInput.Player.Shoot.performed -= OnShoot;
}
private void OnShoot(InputAction.CallbackContext obj)
{
RaycastHit2D raycastHit2D = Physics2D.Raycast(Gun.transform.position, cam.ScreenToWorldPoint(Mouse.current.position.ReadValue()), range); // store info about target
Debug.DrawLine(Gun.transform.position, cam.ScreenToWorldPoint(Mouse.current.position.ReadValue()), Color.white, 1);
Debug.Log("ff");
/*58*/ if (raycastHit2D.transform.gameObject.layer == LayerMask.GetMask("Enemy"))
{
// Destroy enemy
}
}
private void OnHit(InputAction.CallbackContext context)
{
animator_C.SetBool("hit", true);
}
private void OnUnHit(InputAction.CallbackContext context)
{
animator_C.SetBool("hit", false);
}
#endregion
private void Update()
{
OnRun();
}
void OnRun()
{
Vector2 velocity = playerInput.Player.Move.ReadValue<Vector2>();
Debug.Log(velocity);
bool isRunning = Mathf.Abs(velocity.x) > 0;
if (isRunning)
{
animator_C.SetBool("isRunning", true);
transform.rotation = (Quaternion.Euler(new Vector3(0, Mathf.Sign(velocity.x) == 1 ? 0 : 180, 0)));
}
else
{
animator_C.SetBool("isRunning", false);
}
rb2D_C.velocity = velocity * speed;
}
}
This is the error code
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.OnShoot (UnityEngine.InputSystem.InputAction+CallbackContext obj) (at Assets/Scripts/PlayerMovement.cs:58)
UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.CallbackArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.3.0/InputSystem/Utilities/DelegateHelpers.cs:46)
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
NullReferenceException while executing 'performed' callbacks of 'Player/Shoot[/Mouse/leftButton]' UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)
So what was actually happened at line 58? I just couldn't find any clue about this and Why the Shoot event was empty ? I had subscribed the On Shoot function via code :vv
And how to solve this problem, I has been stuck in this for 4 days now :"). Also, this is line 58
if (raycastHit2D.transform.gameObject.layer == LayerMask.GetMask("Enemy"))