Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BoxFlipper · Mar 01, 2014 at 05:21 PM · yield waitforseconds

Yield WaitForSeconds Not causing any pause

I'm trying to make a conversation in my game. To do this I need there to be a pause between the dialogue changes. I tried using yield WaitForSeconds(5); but I got an error. After much research and use of IENumerators, I do not get an error, but it doesn't cause a pause either.

This is my code

 void OnTriggerEnter2D(Collider2D col)
     {
 
 if (col.gameObject.tag == "Couple") {
 
 couple.text = "Who are you?";
 
 StartCoroutine(Wait ());
 
             me.text = ".....";
 
 
         }
     }
     IEnumerator Wait()
     {
         Debug.Log ("waiting");
         yield return new WaitForSeconds(10);
         Debug.Log ("Time's UP!");
         }

The Debug.log returns the "Waiting" but never calls the "Time Up!". I'm really confused :(

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

Answer by Eric5h5 · Mar 01, 2014 at 05:50 PM

I'd guess you're destroying the object before the 10 seconds are up, so the script doesn't exist anymore.

Comment
Add comment · Show 4 · 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 BoxFlipper · Mar 01, 2014 at 05:58 PM 0
Share

Destroying it? But the "..." does appear. There is simply no wait time in between.

avatar image Eric5h5 · Mar 01, 2014 at 07:39 PM 0
Share

Nor should there be any wait, since you're setting the text to "....." immediately. If the "Time's up" is never printed I have to assume the script is being removed or disabled.

avatar image BoxFlipper · Mar 02, 2014 at 12:19 AM 0
Share

well what exactly could be disabling it? I feel like I must be misunderstanding something fundamental about WaitForSeconds. I thought that when I call Wait() in between the lines of Dialogue, it would pause before going on to the next line. Also, where would I be disabling the script? Wouldn't it be in the lines I printed somewhere?

avatar image Eric5h5 · Mar 02, 2014 at 02:43 AM 0
Share

You're not yielding on the coroutine, you're just starting it. https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

avatar image
0

Answer by RabidCabbage · Nov 20, 2014 at 09:41 AM

Startcoroutine creates a new thread (kinda) that runs alongside the current code, it does not halt progression in the main code, only inside the separate Wait function.

If you want something to happen after some time, than it needs to be placed inside the Wait function after the yield.

I can only assume that either:

The "Time's UP!" would be printed after 10 seconds, but you are confused by the instant "....." and not waiting the 10 seconds afterwards.

Or as others have said, the script is being destroyed before the 10 seconds are up.

Try:

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.gameObject.tag == "Couple") {
         couple.text = "Who are you?";

         // The 2 arg's are a bonus suggestion to make Wait() reusable and are
         // not needed to function, I just have OCD for these things. sorry!
         StartCoroutine (Wait (10, "....."));
     }
 }
 
 IEnumerator Wait(int delay, string response)
 {
     Debug.Log ("waiting");
     yield return new WaitForSeconds(delay);
     Debug.Log ("Time's UP!");
     me.text = response;
 }

PS. This code made me think of a game I've been playing recently called "This War of Mine". Have you been playing it too? :)

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

22 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

Related Questions

Is there any reason why I can't use 2 c# scripts on my Player? 3 Answers

Play AudioClips from a List by using yield WaitForSeconds (C#) 1 Answer

Problem with yield 0 Answers

i need to delay an action but my code isnt working 1 Answer

yield return new WaitForSeconds not working 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