Object reference not set to an instance of an object FPS Game

I have this error “Object reference not set to an instance of an object” and i’ve been trying to fix it for 2 hours without success. First my weapon wouldn’t drop (to pickup the new one) and now it drops (yes, the right weapon), but doesn’t pick up the new weapon.

using UnityEngine;
using System.Collections;

public class WeaponManager : MonoBehaviour {
    public GameObject[] weaponsInGame;
    public Rigidbody[] worldModels;
    public GameObject curWeapon;
    public GameObject primaryWeapon;
    public GameObject secondaryWeapon;
    public GameObject meleeWeapon;
    public GameObject[] grenades;
    public GameObject hands;
    public GameObject dropPosition;
    public float switchDelay = 0.5f;
    public AudioClip drawSound;
   
    private GameObject curGrenade;
    private int primaryDropIndex;
    private int secondaryDropIndex;
   
    void Start() {
        audio.clip = drawSound;
        curGrenade = grenades[0];
    }
   
    void Update() {
        if(!animation.isPlaying) {
            if(Input.GetKeyDown(KeyCode.Alpha1) && curWeapon != primaryWeapon && primaryWeapon != null) {
                StartCoroutine(SelectWeapon(primaryWeapon));
            }
            if(Input.GetKeyDown(KeyCode.Alpha2) && curWeapon != secondaryWeapon && secondaryWeapon != null) {
                StartCoroutine(SelectWeapon(secondaryWeapon));
            }
            if(Input.GetKeyDown(KeyCode.Alpha3) && curWeapon != meleeWeapon && meleeWeapon != null) {
                StartCoroutine(SelectWeapon(meleeWeapon));
            }
            if(Input.GetKeyDown(KeyCode.Alpha4) && curWeapon != curGrenade && curGrenade != null) {
                StartCoroutine(SelectWeapon(curGrenade));
            }
        }
       
        if(primaryWeapon != null && primaryWeapon.GetComponentInChildren<GunController>()) {
            primaryDropIndex = primaryWeapon.GetComponentInChildren<GunController>().dropIndex;
        }
       
        if(secondaryWeapon != null && secondaryWeapon.GetComponentInChildren<GunController>()) {
            secondaryDropIndex = secondaryWeapon.GetComponentInChildren<GunController>().dropIndex;
        }
    }
   
    public void DeselectAll() {
        for(int i = 0; i < weaponsInGame.Length; i++) {
            if(weaponsInGame[i].activeSelf) {
                weaponsInGame[i].SetActive(false);
            }
        }
    }
   
    public IEnumerator SelectWeapon(GameObject weapon) {
        animation.CrossFade("Draw", 0.2f);
        audio.clip = drawSound;
        yield return new WaitForSeconds(switchDelay);
        audio.Play();
        DeselectAll();
        weapon.SetActive(true);
        curWeapon = weapon;
    }
   
    public void DropWeapon(int index) {
        Rigidbody drop;
        drop = Instantiate(worldModels[index], dropPosition.transform.position, dropPosition.transform.rotation) as Rigidbody;
        drop.name = worldModels[index].name;
        drop.velocity = GameObject.Find("Main Camera").transform.TransformDirection(new Vector3(0, 0.5f, 3.5f));
    }
   
    public void PickupWeapon(GameObject weapon, WeaponSlot ws) {
        if(ws == WeaponSlot.Primary) {
            if(primaryWeapon != null) {
                DropWeapon(primaryDropIndex);
            }
            primaryWeapon = weapon;
            StartCoroutine(SelectWeapon(primaryWeapon));
        }
        if(ws == WeaponSlot.Secondary) {
            if(secondaryWeapon != null) {
                DropWeapon(secondaryDropIndex);
            }
            secondaryWeapon = weapon;
            StartCoroutine(SelectWeapon(secondaryWeapon));
        }
    }
}

Full error:
NullReferenceException: Object reference not set to an instance of an object
WeaponManager.DeselectAll () (at Assets/MAIN - Zombola/Scripts/Player/WeaponManager.cs:53)
WeaponManager+c__Iterator2.MoveNext () (at Assets/MAIN - Zombola/Scripts/Player/WeaponManager.cs:64)

What is wrong? I selected the right weapons (from the player as it is at “DaBoss FPS Kit (v.1.0.2)”) at “Weapons in game” in the unity inspector.

In Line 53 of your code write:

if(weaponsInGame[i] != null && weaponsInGame[i].activeSelf) {

Some entries in your weaponsInGame array seem to be empty!

1 Like

Thanks, I’ll try, just waiting for unity to respond (sometimes it just doesn’t load the level to play and freezes)

Edit: Yup, it works! Thank you very very much!!

You’re welcome! :slight_smile: