Hi. I’m making a first person shooter game. To shoot I have a gun script and it uses Instantiate. My var Bullet keeps going missing when ever my bullet is destroyed. If it doesn’t make much sense here is the error:
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/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82) Gun+$Shoot$16+$.MoveNext () (at Assets/Gun.js:165)
Here is the bullet script and the destroy bullet script:
//destroy bullet script
function Update () {
Destroy (this.gameObject, 5);
}
//Bullet script
var BulletSpeed : float = 1500;
function Start () {
rigidbody.AddForce(transform.forward * BulletSpeed);
}
Did I do something wrong? Is there a better way to do this? Please help!!!
Thanks,
From, Anon.