- Home /
Destroy a game object after a few seconds of being triggered?
Hey guys. I'm very stuck on something I feel like should not be too hard but nothings working for me. Basically I want the player to enter a box collider and have a text appear (already have that covered), then after about 5 seconds I want the object that triggered the text to be destroyed so it can't be viewed anymore. This is what I have so far:
var Obj : GameObject;
function Start () {
Obj.SetActive(false);
}
function OnTriggerEnter () { Obj.SetActive (true); }
What can I add to this to make the game object (GUI Text) be destroyed after a certain amount of time?
Answer by fafase · Apr 12, 2014 at 06:08 PM
function OnTriggerEnter () {
Obj.SetActive (true);
Destroy(Obj, 5f);
}
you should first read the doc when you get a problem since this is in there.
Thank you. This worked. I'll remember that next time. I'm still getting used to all this haha. Thanks again!
Answer by king_ · Apr 12, 2014 at 06:01 PM
function OnTriggerEnter(){
Invoke("DestroyFunction",5); //5 is 5 seconds}
//destroy function
function DestroyFunction(){
//do everything that needs to be done like destroying the object
}