- Home /
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
None that I know of. Just call a delayed Invoke and call CancelInvoke to cancel it.
I've never even heard of Invokes before lol. I read up on it, and this could definitely work. Thanks
Answer by Eric5h5 · Jul 02, 2011 at 08:06 PM
Dreamblur's comment should have been an answer....
function Start () {
Invoke("DestroyMe", 10.0);
}
function BlahWhatever () {
if (somethingHappens) {
CancelInvoke("DestroyMe");
}
}
function DestroyMe () {
Destroy(gameObject);
}
Answer by jfa257 · Jan 14, 2015 at 06:02 AM
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.
2) A good tutorial about how to properly format the post on this forum is in order, since the interface is far from intuitive.
3) 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.
4) The Destroy Method will occur, no matter what after you call it, whether be invoked or not
5) 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.
Answer by oliver-jones · Jul 02, 2011 at 06:33 PM
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.
He/She was asking if there was a way to cancel a delayed Destroy call. All your code does is delay the Destroy call and then call a delayed Destroy call, which essentially does the same thing as he/she is doing now.
$$anonymous$$y understanding is that once you call Destroy(gameObject, 10), it will be destroyed after 10 seconds, regardless of what's in that if statement after you called it. That shouldn't cancel it. After doing some more research, I think it's apparent that there is no way to cancel it short of writing your own timer and taking care of it yourself. Not really that big of a deal, but I was just curious.