Cancelling Destroy(gameObject, time);

Alright, this might sound like a weird question, but is there a way to cancel a Destroy(gameObject, time) call? For instance, let’s say you wrote

Destroy(gameObject, 10);

So that this gameObject will be destroyed after 10 seconds. 5 seconds later, something happens and you no longer want that gameObject destroyed.

I know that, worst-case, I can create my own timer and call Destroy(gameObject) only when I KNOW that it needs to be destroyed. I’m just hoping that there would be a convenient little call I could make lol.

Thanks

Dreamblur’s comment should have been an answer…

function Start () {
	Invoke("DestroyMe", 10.0);
}

function BlahWhatever () {
	if (somethingHappens) {
		CancelInvoke("DestroyMe");
	}
}

function DestroyMe () {
	Destroy(gameObject);
}
  1. I’m not waking up anything just posting a solution for who ever come across to this post searching for ideas as I did.

    1. A good tutorial about how to properly format the post on this forum is in order, since the interface is far from intuitive.

    2. Double Negatives are a strong logic abstraction when coding, which also turns out to be a strong technique to avoid implicit logic conceptual conflicts in your code, although none of the logic styles should be mandatory when programming anything.

    3. The Destroy Method will occur, no matter what after you call it, whether be invoked or not

    4. Its not the ideal tool have a function which allows you to change your mind about the destruction of the object as many times as you chance the boolean variable?

    I’m familiar with invoke and cancelInvoke and does not solve this, reason as stated in 4). Also the Invoke method is slow, so in a high response flow design is useless anyway, and now that you bring this up I think the only real use of the Invoke CancelInvoke pair other than in a small and/or slow pace script, is to control Coroutines in a more clean-outside way, sort of speak.

    Thank you for recognizing my effort, although the idea was actually to pass on the idea, the implementation is far from universal, it would require a Class with parameters with proper constructors instead of this double negative boolean logic, its just for illustrate my thought on the subject.

Could you use a boolean, like so?:

var stillDestroy : boolean = true;

if(stillDestroy){
   Destroy(gameObject, 10);
}

And then just make stillDestroy false to cancel it.