Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Tim15903 · Apr 05 at 08:51 PM · coroutinewaitforseconds

WaitForSeconds isnt working

Hey, I tried to use WaitForSeconds in my script, but its not working and i cant figure out whats the reason. Without the WaitForSeconds line in the coroutine everything is working fine. But as soon as i try to add this line its not working anymore.

 public class Collision : MonoBehaviour
 {
     public GameObject gameover;
     public GameObject explosion;
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Pylon")
         {
             Destroy(other.gameObject);
             explosion.SetActive(true);
             StartCoroutine(delay());
             GameObject.FindGameObjectWithTag("Player").SetActive(false);
         }
     }
 
    private IEnumerator delay()
    {
         yield return new WaitForSeconds(3);
         gameover.SetActive(true);
         Debug.Log("Game Over");
         yield break;
    }
 }
Comment
Add comment · Show 2
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 Hellium · Apr 05 at 04:30 PM 0
Share

Where is this script attached?

Remember that disabled gameObject can't run coroutines

avatar image Captain_Pineapple · Apr 06 at 06:44 AM 0
Share

what does not working anymore mean? This somewhat implies that there is some kind of error? if yes please share it.


If there is no error please elaborate what not working specifically means here.

1 Reply

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

Answer by GetLitGames · Apr 07 at 01:09 PM

Hellium's comment is most likely the answer. If your Collision script (the script shown) is on the Player's gameobject, it is being disabled and not continuing. These two lines: StartCoroutine(delay()); GameObject.FindGameObjectWithTag("Player").SetActive(false);

The coroutine will stop at your waitforseconds, and the next line executed is disabling the player gameobject, so if your Collision script is on the player, the coroutine is stopped because inactive gameobject do not run coroutines (they die as soon as gameobject becomes inactive)

If you want to get around the problem you can simply use Invoke("YourFunction", 3); and make a function to be called instead of using a coroutine.

Comment
Add comment · Show 2 · 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 Tim15903 · Apr 08 at 06:21 PM 0
Share

Thanks a lot for ur help man. That absolutely makes sense.

avatar image GetLitGames Tim15903 · Apr 09 at 12:49 AM 0
Share

If all you want to do is delay doing something, use Invoke - no need for a coroutine

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

147 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

Related Questions

Coroutines WaitForSeconds() won't work,WaitForSeconds() isn't doing anything 0 Answers

How to implement a "wait" after an animation has finished? 2 Answers

How to make loop with call to IEnumerator actually pause? 2 Answers

Choking on WaitForSeconds coroutine (solved) 3 Answers

wait a second before say path is valid? 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