• 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
Question by edufissure · Jan 20, 2019 at 07:57 PM · scripting problemcoroutinewaitforsecondsienumerator

Problem with coroutine

Hello,

 void Start () {
         gameOverMenuUI.SetActive(false);
     
         player =GameObject.FindObjectOfType<PlayerCharacterManager>();
         _animator= player.GetComponent<Animator>();
     }
 
     void Update () {
         if( player.getGameResult() != 0 )
         {
             Debug.Log("GAme over in GameOverMenu");
                GameOver();
             gameOverMenuUI.SetActive(true);
         }
 
 
     } //End update
 
     public void Restart () {
         //Resume works inversa as Pause
         //In more than one scene we can select to begin again in the same scene
         SceneManager.LoadScene (1);
 
     } //End resume
 
     public void QuitGame () {
         
         Debug.Log("Quitting game....");
         Application.Quit();
      }
      
     private IEnumerator GameOver()
 
     {
         if( player.getGameResult() == 1 ) infoDisplay.text = " Congrats !! You won" ;
         else if( player.getGameResult() == 2 ) infoDisplay.text = " Enemy killed you !! " ;
         else if( player.getGameResult() == 3 ) infoDisplay.text = " You run out of time" ;
 
         _animator.SetInteger("gameResult",1);
          yield return StartCoroutine(Delay(2.0F));
         //player.
         
 
     }    
 
     private IEnumerator Delay(float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         print("WaitAndPrint " + Time.time);
     }

Here the important thing is that i have the gamoovermenuUI disabled, but when i noticed the game ends i want to act some way. The problem is that unity doesnt do the orders in the GameOver method. It doesnt play the animation, nor activate the gameovuermenu. I had to move the gameovermenu to the updatefunction...

What im missing here ? Where is the problem ? I wanted to do: if gameresult =1, put victory in the display info text, play an animation dancing, wait two seconds ( enough to animation being played) and then show the game over menu with the message. If i got an gameresult = 2, put defeat by killed by enemy in text, put an animation of dying, wait to let the animation be played and then show the gameover menu with the text you were killed by enemy...

Summarizing i wanted to put gameOverMenuUI.SetActive(true); after yield return StartCoroutine(Delay(2.0F)); in GameOVer function, but it doesnt work anything i wrote in it

Also would you recommend to put time.scale=0f; like menu pause just to pause all ? Seems nicer or more elegant to show the gameover menu with nothing moving at the background.... ( gameovermenu is a semitransparent background)

Thanks in advance

Comment

People who like this

0 Show 0
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

Answer by xxmariofer · Jan 20, 2019 at 08:18 PM

Dont call coroutines like that, use the StartCoroutine to call the coroutine method, if you put time.timescale to zero it will also stop coroutines so no, also gameOverMenuUI.SetActive(true); will be called before the end of the coroutine, since coroutines are async, if yoou want it to setactive before the coroutine put it inside of the coroutine at the end, if i am missing something else please tell me.

Comment
king_opping

People who like this

1 Show 0 · 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

Answer by edufissure · Jan 20, 2019 at 08:23 PM

But as i know i was using GameOver as public void, but as inside GameOver i called the StartCorroutine it gives an error of compiler, that a void action cant have inside the yield...Thats was the only thing i put GamoIver as enumerator.

The other way i call delay with startcoroutine as you said, as ive done in other scripts... perhaps is GameOver private ?? Dont know...

Thanks

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

175 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 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

Coroutine doesn't work when called from another method. 3 Answers

fps calculation with while loop in coroutine 2 Answers

yield return new WaitForSeconds () not working 1 Answer

Need Help with a Null Reference 1 Answer

OnCollisionEnter as IEnumerator problem (WaitForSecond) 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