- Home /
Why does my Bullet prefab go missing from my gun script when destroyed?
In my First Person Shooter game, I want my bullets to get destroyed every 5 seconds. Instead of destroying itself from the scene, it just gets rid of the prefab on the variable Bullet in my gun script.
Here is the error I get:
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 destroy script:
#pragma strict
function Start ()
{
Destroy (this.gameObject, 5);
}
Thanks and please help!!!
Answer by Em3rgency · Jun 27, 2013 at 07:47 PM
The bullet destroys itself after 5 seconds, but your .MoveNext() is still trying to do its thing (If I read the error correctly, that function is NOT in the bullet itself?). You need to check if the bullet is not null (if it exists) before trying to do things to it.
Your answer
Follow this Question
Related Questions
Why does my bullet prefab get destroyed? 1 Answer
Destroy Turret with machine Gun 0 Answers
Shooting style like enter the dungeon 0 Answers
Enemy Damage problem 1 Answer