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 RoyalSausage · Oct 11, 2017 at 09:23 PM · animationanimatorcoroutinecoroutines

My Coroutine does not get called

Currently have this code: void Update () {

         if (playerHealth <=0 && !isDead)
         {
             levelManager.player.DeathAnimation();
             playerHealth = 0;
                 levelManager.RespawnPlayer();
                 lifeSystem.TakeLife();
                 isDead = true;
                 thetime.ResetTime();
             
                
         }

Above is my Health script, when the player dies it calls the deathAnimation() function off Player

   public void DeathAnimation()
     {
         Debug.Log("1");
         StartCoroutine("DeathAnimationCo");
         Debug.Log("2");
     }
     public IEnumerable DeathAnimationCo()
     {
         Debug.Log("3");
         anim.SetBool("isDead", true);
         Debug.Log("4");
         yield return new WaitForSecondsRealtime(2f);
         
         
 
     }

In the block of code above you see I start my coroutine, in my coroutine I start running my animation and then try to do a yield return to wait for 2 seconds ( so my animation can finish) .

Currently the debug log "3" and "4" do not get called. The animation doesn't start playing at all ( but the code in my Health script does get called, so the player object gets destroyed and reset (as intended)).

Anybody an idea of what I'm doing wrong?

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

4 Replies

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

Answer by DJJD · Oct 11, 2017 at 09:37 PM

As Birdman said, you need to switch the IEnumerable for IEnumarator. In top of that, you should probably, as a good practice, stop writing your coroutines in a string form.

Try StartCoroutine(DeathAnimationCo()); instead. You'll be sure the coroutine will be called this way, else you will get an error (which you would not if you use a string).

Cheers!

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 Bunny83 · Oct 11, 2017 at 09:52 PM 1
Share

Right, Unity doesn't recognise a method that return an IEnumerable as coroutine, though it would still work when used correctly. The IEnumerable interface is just one step above the IEnumerator.

This would work even with IEnumerable as return type:

 StartCoroutine(DeathAnimationCo().GetEnumerator());

Though normally a coroutine should have IEnumerator as return type so you can simply do

 StartCoroutine(DeathAnimationCo());
avatar image RoyalSausage · Oct 12, 2017 at 06:58 PM 0
Share

Thanks a lot guys! This helped!

avatar image abdulrahman13193 · Nov 29, 2020 at 04:06 AM 0
Share

Thank you.

avatar image
5

Answer by Birdman1011395 · Oct 11, 2017 at 09:30 PM

IEnumerator is the return type for coroutines, not IEnumerable.

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 TBART82 · Oct 12, 2017 at 01:01 AM

Change:

 StartCoroutine("DeathAnimationCo");

To:

 StartCoroutine(DeathAnimationCo());

And

 public IEnumerable DeathAnimationCo()
      {
          Debug.Log("3");
          anim.SetBool("isDead", true);
          Debug.Log("4");
          yield return new WaitForSecondsRealtime(2f);
      }

To

 public IEnumerator DeathAnimationCo()
      {
          Debug.Log("3");
          anim.SetBool("isDead", true);
          Debug.Log("4");
          yield return new WaitForSecondsRealtime(2f);
      }
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 Skibur · Oct 12, 2017 at 02:46 AM

Alternative solution, instead of using coroutine, why not use Invoke() instead?

 public void DeathAnimation()
     {
         Debug.Log('3');
         anim.SetBool("isDead", true);
         Debug.Log('4');
         Invoke("DelayMethodHere", 2f);
     }
 
     public void DelayMethodHere()
     {
         //code executes after nth seconds from Invoked.
     }
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 RoyalSausage · Oct 12, 2017 at 07:31 PM 0
Share

While changing it to IEnumerator fixxed it, this worked even better for what I was trying to do. 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

191 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 avatar image avatar image avatar image avatar image 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

2D Animation does not start 1 Answer

Animation Curve or Coroutine? Performance question. 1 Answer

Detecting last frame of animation, without StateMachineBehaviours or Animation Events 0 Answers

WaitUntil and WaitWhile don't work 1 Answer

Returning from Coroutine stops the Update() method. 0 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