• 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 t$$anonymous$$s 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 t$$anonymous$$s 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 t$$anonymous$$s 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 not$$anonymous$$ng magical, and as such, must be "registered" wit$$anonymous$$n Unity (by passing it into StartCoroutine), t$$anonymous$$s 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
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 w$$anonymous$$ch don't have a parent. If a c$$anonymous$$ld is marked with DontDestroyOnLoad but the parent is not, it's useless.

You can do somet$$anonymous$$ng 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Event at audio position? 1 Answer

Stream music in Unity mobile 0 Answers

Ienumerator wait for event 0 Answers

Only the first half of my coroutine works 1 Answer

Music Visualizer to Display inside of GUI 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