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 ToeBeanGames · Jul 11, 2014 at 09:03 PM · audiowaitforsecondsmusicienumerator

IEnumerator issues

I'm having issues with using IEnumerators, more specifically WaitForSeconds. So I have this script that's supposed to play a bgm into, wait until it's done, then play the music loop:

 public IEnumerator playMusic()
 {
     audio.Play ();
     yield return new WaitForSeconds(audio.clip.length);
     audio.clip = musicLoop;
     audio.loop = true;
     audio.Play ();
 }

But what's going on instead is it doesn't play the loop until after twice the length of the intro.

I've had this problem before with stopping the music, playing a jingle, and resuming the music.

Here is the full script if it'll be any help:

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(AudioSource))]
 public class AudioDirector : MonoBehaviour {
     public AudioClip musicIntro;
     public AudioClip musicLoop;
     GameObject musicSource;
     public AudioClip[] Jingles;
     public int JingleType;
 
     // Use this for initialization
     void Start () {
         musicSource = gameObject;
         audio.clip = musicIntro;
         playMusic ();
     
     }
     
     // Update is called once per frame
     void Update () {
         DontDestroyOnLoad (musicSource);
     }
     public IEnumerator playMusic()
     {
         audio.Play ();
         yield return new WaitForSeconds(audio.clip.length);
         audio.clip = musicLoop;
         audio.loop = true;
         audio.Play ();
     }
     public IEnumerator PlayJingle(int jingletype)
     {
         JingleType = jingletype;
         audio.Stop ();
         audio.loop = false;
         audio.clip = Jingles [JingleType];
         audio.Play ();
         yield return new WaitForSeconds(audio.clip.length);
         audio.clip = musicLoop;
         audio.loop = true;
         audio.Play ();
     }
 }
 


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
1
Best Answer

Answer by liortal · Jul 11, 2014 at 09:07 PM

You should be starting a coroutine, passing the IEnumerator into that method.

So instead of:

 playMusic();

you should be calling:

 StartCoroutine(playMusic());

An IEnumerator is nothing magical, and as such, must be "registered" within Unity (by passing it into StartCoroutine), this will make sure Unity will periodically run it, and also let you yield from it and return after parts of it are complete (e.g: wait for some time, etc).

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 Bunny83 · Jul 11, 2014 at 09:32 PM 0
Share

How did i miss that? I guess i didn't look for that possibility since he said that it is actually playing. However that's not possible if you don't use StartCoroutine not a single line of the code inside the method is executed.

avatar image
1

Answer by Kiwasi · Jul 11, 2014 at 09:06 PM

I'm surprised it waited at all.

In C# you need to use StartCouroutine

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 Bunny83 · Jul 11, 2014 at 09:21 PM

Well, what you first should check is:

  • What's the actual length of your intro outside of Unity?

  • What does "musicIntro.length" report?

  • Try starting the playMusic coroutine manually for testing and measure the actual time it takes.

  • Also it makes no sense to call DontDestroyOnLoad every frame. If you call it once it will never be destroyed automatically. The only way is using Destroy(). Keep in mind that DontDestroyOnLoad only works on root objects which don't have a parent. If a child is marked with DontDestroyOnLoad but the parent is not, it's useless.

You can do something like that:

 void Start ()
 {
     musicSource = gameObject;
     DontDestroyOnLoad (musicSource); 
 }
  
 public IEnumerator playMusic()
 {
     Debug.Log("Intro length: " + musicIntro.length);
     var startTime = Time.realtimeSinceStartup;
 
     audio.clip = musicIntro;
     audio.Play ();
 
     Debug.Log("audio.clip.length : " + audio.clip.length);
 
     yield return new WaitForSeconds(audio.clip.length);
     
     var timeTaken = Time.realtimeSinceStartup - startTime;
     Debug.Log("time taken: " + timeTaken);
     
     audio.clip = musicLoop;
     audio.loop = true;
     audio.Play ();
 }
 
 
 void OnGUI()
 {
    if (GUI.Button(new Rect(10,10,100,40), "Start BGM"))
    {
        StartCoroutine(playMusic());
    }
 }

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

24 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

Related Questions

Strange situation about WaitForSeconds 2 Answers

Delay after input? 1 Answer

play audio for few seconds on key hit 1 Answer

Coroutine doesn't work when called from another method. 3 Answers

audio change object size 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