- Home /
How would I make an object disappear after a set amount of time?
As the title says; I'm trying to make a gun script, I have the gun shooting as I wanted, but the cloned rigidbodies stick around, clogging up the game. How would I make them disappear after a set amount of time?
Answer by Joshua · Mar 02, 2011 at 06:00 PM
var lifeTime = 1.0;
function Awake () { Destroy (gameObject, lifeTime); }
Here you go. Next time use the search function because that has been answered several times.
Thanks, I didn't have time today to do a thorough search, but next time I will search a bit further.
Answer by networkZombie · Mar 02, 2011 at 06:01 PM
var destroyTime : Int; // This is the time in seconds function Start(){
yield WaitForSeconds(destroyTime); Destroy(gameObject); } // attach the script to the game object that is supposed to disappear. Good Luck =)
Well, it works, and I thank you for that, but it destroys the prefab and/or the gun ins$$anonymous$$d of the cloned prefab which renders my little gun thing useless.
make sure its attached to the bullet and not to the gun.
I want my bullet to disappear after 5 seconds, this is instant, Any help?
you can just change the yield WaitForSeconds("time here");
Answer by epicpython · Jul 16, 2017 at 01:39 AM
public class Dissapear : MonoBehaviour {
int lifeTime = 10;
public override void Start()
{
StartCoroutine(WaitThenDie());
}
IEnumerator WaitThenDie()
{
yield return new WaitForSeconds(lifeTime);
Destroy(gameObject);
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Best way to throw object 0 Answers
Turning an object into a RigidBody and applying a force on it... 2 Answers
Phisics question! How can I launch this object?!??!?!?!! 0 Answers
object dissappear and reappear 1 Answer