I want to make an object last for only 3 seconds, how would I make a code that destroys it after 3 seconds? (javascript plz)
var timeAlive : float = 0f;
function Update () {
// Destoy after 3 sec
timeAlive = timeAlive + Time.deltaTime;
if (timeAlive >= 3){
Destroy(gameObject);
}
}
There is example in javascript:
You might also want to invoke a destroy function of your own making.
Destroy(gameObject, 3) would also work, but you can’t cancel it.
The below code also works, in addition to being able to use CancelInvoke();
Invoke (DestroyMyObject, 3);
DestroyMyObject()
{
Destroy(gameObject);
}
Well, this would work too:
function Start () {
Destroy (gameObject, 3);
}