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
0
Question by Ryox92 · Jun 28, 2019 at 07:33 AM · coroutinewaitforseconds

Coroutine Not Completely executing

I'm been stuck on this silly bug for some time now and I can't find an answer why it may have happened. I'm trying to do a very simple which is deactivating a panel GameObject using a coroutine (to allow the button SFX to play). Heres what I'm trying to do:

 public class ExampleScript : MonoBehaviour
 {
 
  public GameObject confirmPanel;
 
 public void CancelReturnToMenu()
 {
 StartCoroutine(CancelReturnToMenuTime());
 }
 
 public IEnumerator CancelReturnToMenuTime()
 {
  yield return new WaitForSeconds (0.25f);
 confirmPanel.SetActive(false);
 
 }
 
 }

I'm calling the function CancelReturnToMenu() on my button via an OnClick event in Unity.

I'm not sure why this is not working, I'm not doing anything else. The coroutine is waiting 0.25 sec because I can hear the sound effect, however I'm not sure why the code after that line is not executing.

Thanks for the help

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

2 Replies

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

Answer by xxmariofer · Jun 28, 2019 at 07:42 AM

This is by far the most common error i have seen in unity answers, there are 2 posibilitys, you are destroying the gameobject attached to the examplescript behaviour, or you are desactivating it

Comment
Add comment · Show 6 · 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 Ryox92 · Jun 28, 2019 at 07:56 AM 0
Share

Yeah I've actually seen a couple questions regarding the same issue, but I don't really understand. Just to make sure, the coroutine has to be completely executed then I can deactivate the gameobject with this script attached right??

I thought this might occur only you Destroy/deactivate the gameobject before WaitForSeconds

Edit: Ok I get it, just don't deactivate anything in a coroutine...

avatar image Ryox92 · Jun 28, 2019 at 08:42 AM 0
Share

Can you show me an example of what I should be doing in this case?

avatar image xxmariofer Ryox92 · Jun 28, 2019 at 11:17 AM 0
Share

You have to handle the corroytine from a script thats not attached to the gameobject that you desactivate, you can start the corroytine from anywhere, or desactivate the gameobject AFTER all the yield instructions

avatar image Kennai xxmariofer · Jun 28, 2019 at 11:22 AM 0
Share

Sorry, you are a bit wrong. Coroutine can be handled everywhere except deactivated objects. Coroutine can deactivate itself game object, but coroutine cant activate itself game object, because it dosnt work on deactivated game objects. Every time, when game object is deactivated, it stops all coroutines.

Show more comments
avatar image Ryox92 · Jun 29, 2019 at 09:54 PM 0
Share

Sorry for the late reply, thanks for the help @xxmariofer

avatar image
0

Answer by Kennai · Jun 28, 2019 at 09:28 AM

Hi, @Ryox92! Are you sure, you do not deactivate game object with ExampleScript, when you click button? Maybe your button do 2 things - call func CancelReturnToMenu and deactivate game object with ExampleScript?

Comment
Add comment · Show 1 · 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 Ryox92 · Jun 29, 2019 at 09:56 PM 0
Share

Actually I was also deactivating the GameObject with the ExampleScript, I'll make sure its done somewhere else to avoid such problems in the future. Thanks a lot for your help @$$anonymous$$ennai

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

118 People are following this question.

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

Related Questions

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

yield return new WaitForSeconds () not working 1 Answer

How to get Coroutine's remaining time in WaitForSeconds? 2 Answers

How to make WaitForSeconds work as a repeating/looping timer? 1 Answer

WaitForSeconds bug,Coroutine Bug 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