0

I'm trying to deal damage to a zombie GameObject with a SphereCast that originates from my First Person Camera when the animation from my melee weapon plays.

The problem is that I'm getting a NullReferenceException in the Physics.SphereCast line and I can't figure out why. I've checked every inspector of gameObjects that could be related to this and everything was assigned.

The AiExample Script is attached to every zombie.

public class PlayerAttack : MonoBehaviour
{

    private WeaponManager weaponManager;
    private PlayerAxeWooshSounds axeWooshSounds;

    public float fireRate = 400f;

    public float singleFireRate = 1f;

    private float NextTimeToFire = 0.0f;

    private float lastFired;

    public float damage = 20f;

    private Animator FPanim;

    private GameObject crossHair;

    private bool zoomed;

    private Camera mainCam;

    public int AxeDamage = 25;

    public Transform sphereCastSpawn;

    private WeaponHandler weaponHandler;

    void Start()
    {
        weaponManager = GetComponent<WeaponManager>();

        FPanim = transform.Find(Tags.LOOK_ROOT).transform.Find(Tags.FIRST_PERSON_CAMERA).GetComponent<Animator>();

        crossHair = GameObject.FindGameObjectWithTag(Tags.CROSSHAIR);

        mainCam = Camera.main;

        weaponHandler = GetComponentInChildren<WeaponHandler>();
    }

    void Update()
    {
        WeaponShoot();
        ZoomInAndOut();
    }

    void WeaponShoot()
    {
            if(Input.GetMouseButtonDown(0))
            {
                if(weaponManager.GetCurrentSelectedWeapon().tag == (Tags.AXE_TAG))
                {
                    weaponManager.GetCurrentSelectedWeapon().ShootAnimation();
                    RaycastHit hit;
                    if(Physics.SphereCast(origin: sphereCastSpawn.position, 0.5f, sphereCastSpawn.TransformDirection(Vector3.forward),
                        out hit, weaponHandler.zombieLayer))
                    {
                        hit.transform.GetComponent<AiExample>().DamageZombie(AxeDamage);
                    }
                }
            }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
L1NTHALO
  • 23
  • 5
  • Is the exception happening *on* that line, or does the stack trace point to something deeper which was *invoked* by that line? When the exception is thrown, what is the observed runtime value of `sphereCastSpawn`? I assume `Physics` and `Vector3` are types on which you're referencing static members? Can you confirm that? – David Sep 29 '21 at 18:08
  • The console says that it's happening on the line where the if statement starts. What's a runtime value and where can I see it? – L1NTHALO Sep 29 '21 at 18:20
  • A runtime value is the value held by a variable when the incident occurs during application execution. You can see it by debugging and using breakpoints, stepping through the application code as it executes, maybe configuring the debugger to pause on exceptions, etc. See: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/328193) – David Sep 29 '21 at 18:21
  • Ok, I actually fixed the issue with the debugger. I have never heard of a debugger before and it's a really helpful tool. Thank you very much for helping me :) – L1NTHALO Sep 30 '21 at 17:38

0 Answers0