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 /
  • Help Room /
avatar image
0
Question by SteliosDaVitzi · Jul 23, 2016 at 11:44 PM · coroutinewaitforsecondsienumerator

IEnumerator not working.WaitForSeconds never continiues.

When my character dies a canvas pops up and i want the sound to stop after 2 seconds.I've tried IEnumerator with coroutine but it seems that it never executes.The Debug.Log ("Start") appears in console,but the End never executes.Any ideas? Here is the code.

public void makedead ()

 {

     Destroy(gameObject);

     Instantiate (DeathFXPrefab, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint (DeathSound, transform.position);
     health = 0;

     tryAgain.enabled = true;
     Time.timeScale = 0.3f;
     StartCoroutine (soundpause (2.0f));

 }

 IEnumerator soundpause(float sec)
 {
         Debug.Log ("Start");
         yield return new WaitForSeconds (sec);
         AudioListener.pause = true;
         Debug.Log ("End");
 }
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 WillNode · Jul 24, 2016 at 02:07 AM

I'm sure 99% of your problem comes from...

 Destroy(gameObject);

You are destroying the game object, while it runs a coroutine. That's just make the coroutine stopped at the end of the frame when starting a coroutine.

One of the solution is put that destory function after the coroutine finished.

      {
          Instantiate (DeathFXPrefab, transform.position, transform.rotation);
          AudioSource.PlayClipAtPoint (DeathSound, transform.position);
          health = 0;
          tryAgain.enabled = true;
          Time.timeScale = 0.3f;
          StartCoroutine (soundpause (2.0f));
      }
      IEnumerator soundpause(float sec)
      {
              Debug.Log ("Start");
              yield return new WaitForSeconds (sec);
              AudioListener.pause = true;
                Destroy(gameObject);
             Debug.Log ("End");
      }

FYI, Time.timeScale affects WaitForSeconds timing. it will not really 2 seconds, but 2 / 0.3 seconds, which is much longer time.

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 SteliosDaVitzi · Jul 24, 2016 at 11:20 AM 0
Share

Thanks for the answer,a part of the problem was this.I did exactly as you said and i deactivated time.timescale for the experiment.The coroutine completes it's circle,but it multiplies the DeathFx,and DeathSound so many times that makes the game nearly crash (my pc too).Sorry,but i'm new to all this and i have not fully understood the coroutine mechanisms.

avatar image
0

Answer by SteliosDaVitzi · Jul 24, 2016 at 01:27 PM

Thanks to your answer,i finally did it!I placed the Coroutine in a different void which i called in OnTriggerEnter,and because the character had time to die again,i deactivated it's mesh renderer,speed,and collider.here is the script:

void Awake()

 {

     rb = GetComponent<Rigidbody> ();
     tryAgain.enabled = false;
     rend = GetComponent<Renderer> ();
     rend.enabled = true;
     isDead = false;
     coll.enabled = true;


 }

 void FixedUpdate ()

 {

     transform.Translate (Input.acceleration.x * speedx * Time.deltaTime, 0, +Input.acceleration.y * speedy * Time.deltaTime);
     rb.AddForce(0,0,0);
     
 }
     
 public void makedead ()
 {
     health = 0;
     Instantiate (DeathFXPrefab, transform.position, transform.rotation);
     AudioSource.PlayClipAtPoint (DeathSound, transform.position);
     rend.enabled = false;
     isDead = true;
     speedx = 0;
     speedy = 0;
     coll.enabled = false;
 }

 void Coroutine()
 {
     StartCoroutine (death (2.0f));
 }

 IEnumerator death(float sec)
 {
     Debug.Log ("Start");
     yield return new WaitForSeconds (sec);
     Destroy(gameObject);
     tryAgain.enabled = true;
     AudioListener.pause = true;
     Time.timeScale = 0.1f;
     Debug.Log ("End");
 }


 public void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Enemy") {
         Coroutine ();
         makedead ();
     }
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

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

66 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

Related Questions

Could not load source 'Coroutines.cs': No source available. 1 Answer

Trying to make my code pause using coroutines, what am i doing wrong? 2 Answers

Add points every five seconds 1 Answer

Help with coroutines 1 Answer

How to force IEnumerator methods to be called only with StartCoroutine 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