Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by FPSUsername, Jan 20, 2015.

  1. FPSUsername

    FPSUsername

    Joined:
    Aug 12, 2014
    Posts:
    5
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeaponManager : MonoBehaviour {
    5.     public GameObject[] weaponsInGame;
    6.     public Rigidbody[] worldModels;
    7.     public GameObject curWeapon;
    8.     public GameObject primaryWeapon;
    9.     public GameObject secondaryWeapon;
    10.     public GameObject meleeWeapon;
    11.     public GameObject[] grenades;
    12.     public GameObject hands;
    13.     public GameObject dropPosition;
    14.     public float switchDelay = 0.5f;
    15.     public AudioClip drawSound;
    16.    
    17.     private GameObject curGrenade;
    18.     private int primaryDropIndex;
    19.     private int secondaryDropIndex;
    20.    
    21.     void Start() {
    22.         audio.clip = drawSound;
    23.         curGrenade = grenades[0];
    24.     }
    25.    
    26.     void Update() {
    27.         if(!animation.isPlaying) {
    28.             if(Input.GetKeyDown(KeyCode.Alpha1) && curWeapon != primaryWeapon && primaryWeapon != null) {
    29.                 StartCoroutine(SelectWeapon(primaryWeapon));
    30.             }
    31.             if(Input.GetKeyDown(KeyCode.Alpha2) && curWeapon != secondaryWeapon && secondaryWeapon != null) {
    32.                 StartCoroutine(SelectWeapon(secondaryWeapon));
    33.             }
    34.             if(Input.GetKeyDown(KeyCode.Alpha3) && curWeapon != meleeWeapon && meleeWeapon != null) {
    35.                 StartCoroutine(SelectWeapon(meleeWeapon));
    36.             }
    37.             if(Input.GetKeyDown(KeyCode.Alpha4) && curWeapon != curGrenade && curGrenade != null) {
    38.                 StartCoroutine(SelectWeapon(curGrenade));
    39.             }
    40.         }
    41.        
    42.         if(primaryWeapon != null && primaryWeapon.GetComponentInChildren<GunController>()) {
    43.             primaryDropIndex = primaryWeapon.GetComponentInChildren<GunController>().dropIndex;
    44.         }
    45.        
    46.         if(secondaryWeapon != null && secondaryWeapon.GetComponentInChildren<GunController>()) {
    47.             secondaryDropIndex = secondaryWeapon.GetComponentInChildren<GunController>().dropIndex;
    48.         }
    49.     }
    50.    
    51.     public void DeselectAll() {
    52.         for(int i = 0; i < weaponsInGame.Length; i++) {
    53.             if(weaponsInGame[i].activeSelf) {
    54.                 weaponsInGame[i].SetActive(false);
    55.             }
    56.         }
    57.     }
    58.    
    59.     public IEnumerator SelectWeapon(GameObject weapon) {
    60.         animation.CrossFade("Draw", 0.2f);
    61.         audio.clip = drawSound;
    62.         yield return new WaitForSeconds(switchDelay);
    63.         audio.Play();
    64.         DeselectAll();
    65.         weapon.SetActive(true);
    66.         curWeapon = weapon;
    67.     }
    68.    
    69.     public void DropWeapon(int index) {
    70.         Rigidbody drop;
    71.         drop = Instantiate(worldModels[index], dropPosition.transform.position, dropPosition.transform.rotation) as Rigidbody;
    72.         drop.name = worldModels[index].name;
    73.         drop.velocity = GameObject.Find("Main Camera").transform.TransformDirection(new Vector3(0, 0.5f, 3.5f));
    74.     }
    75.    
    76.     public void PickupWeapon(GameObject weapon, WeaponSlot ws) {
    77.         if(ws == WeaponSlot.Primary) {
    78.             if(primaryWeapon != null) {
    79.                 DropWeapon(primaryDropIndex);
    80.             }
    81.             primaryWeapon = weapon;
    82.             StartCoroutine(SelectWeapon(primaryWeapon));
    83.         }
    84.         if(ws == WeaponSlot.Secondary) {
    85.             if(secondaryWeapon != null) {
    86.                 DropWeapon(secondaryDropIndex);
    87.             }
    88.             secondaryWeapon = weapon;
    89.             StartCoroutine(SelectWeapon(secondaryWeapon));
    90.         }
    91.     }
    92. }
    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+<SelectWeapon>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.
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    In Line 53 of your code write:

    Code (CSharp):
    1. if(weaponsInGame[i] != null && weaponsInGame[i].activeSelf) {
    Some entries in your weaponsInGame array seem to be empty!
     
    FPSUsername likes this.
  3. FPSUsername

    FPSUsername

    Joined:
    Aug 12, 2014
    Posts:
    5
    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!!
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    You're welcome! :)