Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Kith · Jul 02, 2011 at 06:15 PM · destroyfunctioncallcancel

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamblur · Jul 02, 2011 at 06:30 PM 5
Share

None that I know of. Just call a delayed Invoke and call CancelInvoke to cancel it.

avatar image Kith · Jul 02, 2011 at 06:39 PM 0
Share

I've never even heard of Invokes before lol. I read up on it, and this could definitely work. Thanks

3 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamblur · Jul 02, 2011 at 06:36 PM 2
Share

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.

avatar image Kith · Jul 02, 2011 at 06:37 PM 0
Share

$$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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Efficient way for calling a function once 3 Answers

How can I call a method from another script? 3 Answers

Call Function with Collision-parameter 1 Answer

Coroutine cannot be automatically started from a static function 1 Answer

Variable inside a function's NAME - does it work? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges