Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

FPS Modular System Errors, again

I sent a similar problem here a few days ago, but 2 days ago a strange error happened:

NullReferenceException: Object reference not set to an instance of an object

WeaponManager.Update () (at Assets/Scripts/WeaponManager.cs:57)

The script is shown in my previous post, but i'll send it here too:

using System.Collections.Generic;

using System.Collections;

using UnityEngine;


public class WeaponManager : MonoBehaviour

{

  public float pickupRange;

  public float pickupRadius;


  public int weaponLayer;


  public Transform weaponHolder;

  public Transform playerCamera;


  private bool isWeaponHold;

  private Weapon holdWeapon;


  private void Update()

  {

    if (isWeaponHold)

    {

      if (Input.GetKeyDown(KeyCode.Q))

      {

        holdWeapon.Throw(playerCamera);

        holdWeapon = null;

        isWeaponHold = false;

      }

    }

    else if (Input.GetKeyDown(KeyCode.E)) {

      var hitList = new RaycastHit[256];

      var HitNumber = Physics.CapsuleCastNonAlloc(playerCamera.position, playerCamera.position + playerCamera.forward * pickupRange, pickupRadius, playerCamera.forward, hitList);


      var realList = new List<RaycastHit>();

      for (var i = 0; i < HitNumber; i++) {

        var hit = hitList[i];

        if (hit.transform.gameObject.layer != weaponLayer) continue;

        if (hit.point == Vector3.zero)

        {

          realList.Add(hit);

        }

        else if (Physics.Raycast(playerCamera.position, hit.point - playerCamera.position, out var hitInfo, hit.distance + 0.1f) && hitInfo.transform == hit.transform)

        {

          realList.Add(hit);

        }

      }


      if (realList.Count == 0) return;


      realList.Sort((hit1, hit2) => {

        var dist1 = GetDistanceTo(hit1);

        var dist2 = GetDistanceTo(hit2);

        return Mathf.Abs(dist1 - dist2) < 0.001f ? 0 : dist1 < dist2 ? -1 : 1;

      });


      isWeaponHold = true;

      holdWeapon = realList[0].transform.GetComponent<Weapon>();

      holdWeapon.Pickup(weaponHolder);

    }

  }


  private float GetDistanceTo(RaycastHit hit)

  {

    return Vector3.Distance(playerCamera.position, hit.point == Vector3.zero ? hit.transform.position : hit.point);

  }

}


The line 57 is "holdWeapon.Pickup(weaponHolder);". the weaponHolder is set, and the holdWeapon.Pickup goes to another script, which is picking up/throwing the weapon.


I don't know why is this happening, and if someone would know the answer, i'd be really grateful.

Answers

Sign In or Register to comment.