I am making a game based on design of super crate box: player collides with box and it chooses a random weapon.
The problem is if the shotgun bullet is active while I shoot again using another gun, I get the exception (I will obviously later make the bullet disappears but the problem also occurs even if the bullet disappears).
I need to press the key really fast for it to happen so I assume the exception only happens when the shotgun bullet is active.
Exception scenario:
- player picks up shotgun
- player fire bullet (bullet doesn't disappear for testing purposes)
- player pick up another gun
- while bullet is still active, player presses control again to fire bullet => Exception
The line where the exception occurs is the first instance of this Transform cameraTransform = player.transform;.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class FireGun : MonoBehaviour
{
     //used to hold the guns user is currently holding
     public List Guns = new List();
     //to acceess pickupbx
     public PickupBx pbx;
     //holds the current name weapon user is holding
     private string weaponFireType;
     //this will get the bullet prefab
     public GameObject BulletPrefab;
     public GameObject shotGunBulletPrefab;
     //bullet position on player
     public GameObject bulletPosition;
     public GameObject shotgunBulletPosition;
     //the player
     public GameObject player;
     //this is the bullet that will be instantiated in scene (rpg/machine      gun)
     public GameObject bulletInScene;
     //this is for the shotgun bullet that will be instantiated
     public GameObject ShotgunBulletInScene;
     //used to make shooty shoot once every few seconds
     private bool shootShottyOnce = false;
     void Start()
     {
          pbx = GetComponent();
     }
     // Update is called once per frame
     void Update()
     {
          if (GetComponent().flipX == true)
          {
               bulletPosition.transform.position = new Vector2(-92, 0);
          }
          else if (GetComponent().flipX == false)
          {
               bulletPosition.transform.position = new Vector2(92, 0);
          }
          AddObjectToList(pbx.guns, Guns);
          checkWeaponType();
     }
     //this method will loop through the gameobjects in pbxguns list (first parameter is the guns list in the pickupBx script), (myList is the list in this script)_
     void AddObjectToList(List pbxGunsList, List myList)
     {
          //loop through all gaeobjects in the pbxGunsList list
          foreach (GameObject go in pbxGunsList)
          {
               //if this list in this script doesn't contain the gameObjects      from the other script
               if (!myList.Contains(go))
               {
                    //clear the list to make sure we don't get duplicates and have weapons in one 1 element
                    myList.Clear();
                    //add the gun to our list
                    myList.Add(go);
               }
          }
     }
     //check weapon the player is holding
     void checkWeaponType()
     {
          //loop through the guns and set the string to weapon player is currently holding
          for (int i = 0; i < Guns.Count; i++)
          {
               if (Guns[i].name == "weapons_0(Clone)")
               {
                    weaponFireType = "MachineGun";
               }
               else if (Guns[i].name == "weapons_1(Clone)")
               {
                    weaponFireType = "Shotgun";
               }
               else if (Guns[i].name == "weapons_2(Clone)")
               {
                    weaponFireType = "RPG";
               }
          }
          //check if user pressed key and check that guns is not empty ( reason shotgun is inputed here is because the shotgun will shoot differently to the other guns
          if (Input.GetKeyDown(KeyCode.LeftControl) && Guns.Count != 0 && weaponFireType != "Shotgun")
          {
               //make a new gameObject which will hold the bullet in the scene it will instantiate a bullet at the position of the bulletPosition empty gameObject.
               bulletInScene = (GameObject)Instantiate(BulletPrefab, bulletPosition.transform.position, Quaternion.identity);
               //the transform of the player
               Transform cameraTransform = player.transform;
               //Makes the player the parent of the GameObject currentGun
               bulletInScene.transform.SetParent(cameraTransform, false);
          }
          if (Input.GetKeyDown(KeyCode.LeftControl) && weaponFireType == "Shotgun")
          {
               ShotgunBulletInScene = (GameObject)Instantiate(shotGunBulletPrefab, shotgunBulletPosition.transform.position, Quaternion.identity);
               //the transform of the player
               Transform cameraTransform = player.transform;
               //Makes the player the parent of the GameObject currentGun
               ShotgunBulletInScene.transform.SetParent(cameraTransform, false);
          }
          foreach (Transform t in transform)
          {
               if (bulletInScene != null)
               {
                    if (t.name.Contains("Bullet1(Clone)"))
                    {
                         transform.Find("Bullet1(Clone)").transform.parent = null;
                         if (GetComponent().flipX == true && weaponFireType == "MachineGun")
                              bulletInScene.GetComponent().AddForce(Vector2.left * 7500);
                         if (GetComponent().flipX == false && weaponFireType == "MachineGun")
                              bulletInScene.GetComponent().AddForce(Vector2.right * 7500);
                         if (GetComponent().flipX == true && weaponFireType == "RPG")
                              bulletInScene.GetComponent().AddForce(Vector2.left * 4500);
                         if (GetComponent().flipX == false && weaponFireType == "RPG")
                              bulletInScene.GetComponent().AddForce(Vector2.right * 4500);
                    }
               }
          }
          foreach (Transform t in transform)
          {
               if(ShotgunBulletInScene != null)
               {
                    if (t.name.Contains("ShotgunBullet1(Clone)"))
                    {
                         Debug.Log("shotty test");
                         if (shootShottyOnce == false)
                         {
                              StartCoroutine(DestroyShottyBullet());
                         }
                    }
               }
          }
     }
     IEnumerator DestroyShottyBullet()
     {
          shootShottyOnce = true;
          yield return new WaitForSeconds(0.5f);
          Destroy(ShotgunBulletInScene);
          shootShottyOnce = false;
     }
}
 
     
    