- Home /
Destroy the gameObject after seconds when not hit.
Hi Guys, I was trying to destroy a game object if it was not hit. Here's my algorithm.
Objects moving towards the character.
The character has to hit the objects.
If hit, destroy immediately, and increment score.
If not hit, destroy the object after 5 seconds.
Here's my code:
var destroyed : boolean = false;
var explosion : Transform;
function OnCollisionEnter (myObject : Collision) { if(myObject.gameObject.name=="Cube(Clone)"){
Destroy(myObject.gameObject);
destroyed = true; Instantiate(explosion, transform.position, transform.rotation); gameObject.Find("GUI Score").SendMessage("React");
Debug.Log("Score!");
yield WaitForSeconds(1);
gameObject.active=true;
} //explosion =
else if(myObject.gameObject.name=="Cube(Clone)" && destroyed){
Destroy(myObject.gameObject, 5);
destroyed = true; }
}
---------------------
Everything is working fine, even the score. But I just can't figure out how will I destroy the object if not hit because it will stay on the game as long as the scene is not over.
Thanks! Hope you can help me with this :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613081508im_/https://answers.unity.com/themes/thub/images/avi.jpg)