MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:44) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:53) Gun.Update () (at J:/W.A.R/Assets/Scripts/Gun.cs:16)
thats the error i get and here my code for the gun class
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public GameObject bullet = null;
public float bulletSpeed = 500f;
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0)) {
/*
* Instantiate the bullet using the prefab and store it into the shot variable
*/
GameObject shot = GameObject.Instantiate(bullet, transform.position + (transform.forward * 2), transform.rotation) as GameObject;
/*
* Adding force
*/
shot.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}