- Home /
Why does my bullet prefab get destroyed?
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.
Answer by Julien-Lynge · Jun 27, 2013 at 07:39 PM
Every update you're telling the bullet to destroy itself 5 seconds from now, so in those 5 seconds you're potentially telling it to destroy it self hundreds of times.
Just move the Destroy() to the Start() function so it only gets called once.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy object last on application close. 1 Answer
NullReferenceException while trying to access a Component 4 Answers
Why does my Bullet prefab go missing from my gun script when destroyed? 1 Answer
Destroyed object not comparing to null 5 Answers