0

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

enter image description here

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"))
derHugo
  • 83,094
  • 9
  • 75
  • 115
Khoaoaoa
  • 23
  • 6
  • 1
    sorry I will edit it – Khoaoaoa Sep 12 '22 at 17:07
  • 2
    Basic debugging: Set a breakpoint on that line and inspect the objects in question. Either raycastHit2D is null, or LayerMask is null – LarryBud Sep 12 '22 at 17:12
  • 2
    Use a debugger to step through the code and see which object is null. – fredrik Sep 12 '22 at 17:12
  • I solve it, I didn't create the Enemy Layer Mask, thank you @LarryBud – Khoaoaoa Sep 12 '22 at 17:18
  • 1
    What promises you there will always be a valid `raycastHit2D`? If your raycast doesn't hit anything then there will be no `raycastHit2D.transform` => you rather want to add a check `if(raycastHit2D.transform){ ... }` ... and in general rather pass in an actual [`LayerMask`](https://docs.unity3d.com/ScriptReference/LayerMask.html) into the [`Physics2D.Raycast`](https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html) itself so it won't even hit anything that is not on the desired layer – derHugo Sep 13 '22 at 11:08
  • 1
    @LarryBud ^^ nope ... [`RaycastHit2D`](https://docs.unity3d.com/ScriptReference/RaycastHit2D.html) is a **struct** which is never `null` and [`LayerMask.GetMask`](https://docs.unity3d.com/ScriptReference/LayerMask.GetMask.html) is a **static** method (`LayerMask` itself is also a **struct** btw) .. what happens is rather that the raycast doesn't hit anything so `raycastHit2D` is an invalid struct with default values => all references will be `null` in there – derHugo Sep 13 '22 at 11:14

0 Answers0