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 whackamuffin · Jul 03, 2014 at 06:23 PM · waitforseconds

Trying to make WaitForSeconds work

Hey guys,

Basically I have a trigger and I want to play a sound upon entering it... 5 seconds later I want to play a different sound... but I cant seem to get WaitForSeconds to work in C#. Any suggestions?

Thanks, heres my code.

using UnityEngine; using System.Collections;

 public class PlaySound : MonoBehaviour {
 
     public AudioSource vOne;
     public AudioSource vTwo;
     public bool played;
     
 
     void OnTriggerEnter(Collider other) {
         if (played != true) 
         {
             played = true;
             vOne.Play();
             StartCoroutine(Example());
             vTwo.Play();
         }
     }
 
     IEnumerator Example() {
         yield return new WaitForSeconds(5);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Jul 03, 2014 at 06:27 PM

When you call StartCoroutine(), it runs through a single iteration of your coroutine and then moves to the next line of code, in this case vTwo.Play. If you want to play vTwo after the coroutine is finished, move it under the "yield return new WaitForSeconds(5)" line. Each frame, Example will be called, and it will continue yielding inside the WaitForSeconds(5) method and finally move to the lines below that after 5 seconds. The point of the coroutine is to allow small segments of code to be run between frames without halting the main thread. If OnTriggerEnter were an IEnumerator, you could yield the StartCoroutine(Example()) which would wait for Example to finish before moving to the next line, but it's not in this case.

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
0

Answer by timvanderweijde · Jul 03, 2014 at 06:41 PM

The coroutine is working, however within the OnTriggerEnter method everything is directly in order executed.

You could use yield return StartCoroutine(Example());

You have to make the OnTriggerEnter method also use the IEnumerator, which is a little bit strange.

Try to move your code to another method like "IEnumerator PlaySound()" e.d. and then but the code there.

 if (played != true) 
         {
             StartCoroutine(PlaySound());
         }
 IEnumerator PlaySound()
     {
         played = true;
         vOne.Play();
         yield return StartCoroutine(Example());
         vTwo.Play();
     }
 
     IEnumerator Example() {
         yield return new WaitForSeconds(5);
         Debug.Log(System.DateTime.Now);
         Debug.Log("end coroutine");
     }



I also liked the information from this link. Really nice read. https://caliburnmicro.codeplex.com/wikipage?title=IResult%20and%20Coroutines

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
0

Answer by Xyexs · Jul 03, 2014 at 07:29 PM

I am not very good with this, but from what I understand coroutines are threads (...ish).

They are exectuted simultaneously as other parts of the program. When you start the coroutine you make a thread that waits for 5 seconds and then does nothing.

A way to solve this would be putting the sounds inside the end of the coroutine;

 IEnumerator Example() {
     vOne.play(); 
     yield return new WaitForSeconds(5);
     vTwo.play(); 
 }

I would love if anyone let me know if i am wrong in this.

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 Kiwasi · Jul 03, 2014 at 07:30 PM 1
Share

Its not an actual thread, but your solution is valid.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Csharp: how to make script wait for x seconds 3 Answers

About WaitForSeconds 1 Answer

interrupt WaitForSeconds() ? 1 Answer

Toggling my minimap 1 Answer

Why isn't this script working? (Beginner in coding) 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