Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Callan S. · Aug 27, 2012 at 04:06 AM · destroyinvokerepeatingcancel

Destroy cancels InvokeRepeating?

Does it not matter what order/where you put Destroy? I have this code

 if (touched==1) 
 {
 audio.PlayClipAtPoint(gemgotsound,transform.position);
 InvokeRepeating("gotonextlevel",1.5,1);
 Destroy (this.gameObject,0.1); 
 }

The sound plays fine. But the InvokeRepeating only works if I comment out Destroy (gameObject)? Even if I set Destroy to happen after a time period, it still doesn't work. I could think of some ugly coding methods to deal with this, but what is the elegant solution?

Comment
Add comment
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

1 Reply

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

Answer by jorjdboss · Aug 27, 2012 at 08:32 AM

I don't know what you're trying to do exactly by calling InvokeRepeating() to switch levels. If you destroy the gameobject, all attached components will also be destroyed. Since the function gotonextlevel() resides in the same script which is a component of the main gameobject, Invoke repeating wont work anytime after the gameobject is destroyed. The function gotonectlevel() will be called once and thats all. Would be helpful if you mentioned why exactly you're calling InvokeRepeating() to switch levels.

Comment
Add comment · Show 3 · 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 Callan S. · Aug 27, 2012 at 08:50 AM 0
Share

Yeah, I latter realised - I was thinking it was a function that should just be called and do it's thing - I forgot it's thing is to call another function that's inside what is being destroyed. I'm using invoke because I want the level to end a second or so after the gem is collected, not at the very instant it's collected. I may delete this question if it's not a very good one.

avatar image jorjdboss · Aug 27, 2012 at 10:44 AM 0
Share

Did you know that if you're using Application.LoadLevel(), all the gameobjects in the current scene are destroyed before the new level is loaded?

By the way, your problem can be solved easily:

if (touched==1) { audio.PlayClipAtPoint(gemgotsound,transform.position); StartCoroutine(waitAndLoadLevel()); }

IEnumerator waitAndLoadLevel() { yield return new WaitForSeconds(3); // waits 3 seconds and then loads level gotonextlevel(); // assu$$anonymous$$g that you do Application.LoadLevel() inside gotonextlevel() Destroy (this.gameObject,0.1); }

avatar image Callan S. · Aug 27, 2012 at 11:43 AM 0
Share

Yeah, I'm fine with everything being destroyed. I don't actually want the repeating part - I just want that I can put a delay in by using it. I will try your code at some point, though the formattings been lost. I made up something that works, but yours is certainly tighter looking code. Thanks.

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

8 People are following this question.

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

Related Questions

Destroying an object with running coroutine 2 Answers

Cancelling Destroy(gameObject, time); 3 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Spawned Enemies: How to Destroy child objects inside their own attached scripts without Destroying the parent object for InvokeRepeating?? 1 Answer

Destorying despite IF Statement 1 Answer


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