- Home /
Gameobject is not being destroyed
Hey guys, I am trying to (after some seconds) destory a gameobject in my game. I have put debug code segments in my code and it says that my object is destoryed, but it is still in game. Here is my code:
var timenight : int; //5
var deathchurch: Transform;
function Start () {
Debug.Log("Time Start for DeathChurch");
yield new WaitForSeconds(timenight);
Debug.Log("Time Ends");
Destroy (deathchurch);//Destroys object blocking path
Debug.Log("DeathChurch Gone");
}
How do I get this gameObject to be destroyed after a few seconds! Any help would be great, thanks!
Answer by Linus · Jun 27, 2013 at 01:37 AM
You can have the timer built into the Destroy function. And you destroy the gameObject not the transform. I think transform.gameObject Destroy(transform.gameObject,5) could also be used for destruction
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
// Kills the game object in 5 seconds after loading the object
Destroy (gameObject, 5);
In your example:
var timenight : float = 5.0; //Value needs to be set, and be a float not int
var deathchurch: Transform;
function Start () {
deathchurch = transform; //The var needs to be set, not just declared. Should not be needed in this example though
Debug.Log("Time Start for DeathChurch");
Destroy (gameObject, timenight);//Destroys object blocking path
//Destroy (deathchurch.gameObject, timenight); //could also work I believe
Debug.Log("DeathChurch Gone");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
"Destroy Game Object" Not Destroying Game Object 3 Answers
Destroy on Collision? 1 Answer
Destroy GameObject Variable? 3 Answers
Trying to make a simple inventory: 0 Answers