Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by rachmadrin · Jul 17, 2016 at 02:23 AM · gameobjectaudiosourceplayclipsounds

How to Play sequentially more than one AudioSource in one game object ?

I have 3 AudioSource component in main camera. There was : audio1.mp3 , audio2.mp3, audio3.mp3. And i want to play it sequentially the first i want to play audio1.mp3, after it end then it'll automatically play audio2.mp3, and so on. I don't have any idea about this.

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

1 Reply

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

Answer by TBruce · Jul 17, 2016 at 03:20 AM

Here is a class that will allow you to do just that. It has two public lists, one is of the AudioClips and the other is for the AudioSources.

The class is flexible in that the only main requirement is that you have to set the AudioClip list soundList . This can be set to as many AudioClips as you want, it is not limited to three.

This class could also be modified to use just one AudioSource. Is there any reason why you need more than one?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AudioManager : MonoBehaviour
 {
     [Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
     public List<AudioClip> soundList = new List<AudioClip>();
 
     [Tooltip("List of AudioSource components. This list does not need to be set.")]
     public List<AudioSource> audioSourceList = new List<AudioSource>();
     
     [Tooltip("This variriable resets the audioSourceList and creates a separate AudioSource for each clip if set to true.")]
     public bool restAudioSourceList = false;
     
     [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
     public bool playOnAwake = false;
 
     [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
     public bool loop = false;
 
     private bool soundsGoodToPlay = false;
     
     void Start ()
     {
         if (soundList.Count > 0)
         {
             // this loop just make sure that there are valid AudioClips in the soundList
             for (int i = (soundList.Count - 1); i >= 0; i--)
             {
                 if (soundList[i] == null)
                 {
                     soundList.RemoveAt(i);
                 }
             }
             
             if (soundList.Count > 0)
             {
                 if ((restAudioSourceList) || (audioSourceList.Count == 0) || (audioSourceList.Count < soundList.Count))
                 {
                     audioSourceList.Clear();
                     for (int i = 0; i < soundList.Count; i++)
                     {
                         AudioSource source = gameObject.AddComponent<AudioSource>();
                         source.playOnAwake = playOnAwake;
                         source.loop = loop;
                         audioSourceList.Add(source);
                     }
                 }
                 soundsGoodToPlay = (soundList.Count > 0);
             }
         }
     }
 
     void PlaySequentialSounds()
     {
         StartCoroutine(PlaySounds());
     }
 
     IEnumerator PlaySounds()
     {
         if (soundList.Count > 0)
         {
             for (int i = 0; i < soundList.Count; i++)
             {
                 audioSourceList[i].PlayOneShot(soundList[i]);
 
                 // wait for the lenght of the clip to finish playing
                 yield return new WaitForSeconds(soundList[i].length);
             }
         }
         yield return null;
     }
 }

The following class does the same as the one above but uses only one AudioSource

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class AudioManager : MonoBehaviour
 {
     [Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
     public List<AudioClip> soundList = new List<AudioClip>();
 
     [Tooltip("The AudioSource component to play the AudioClips.")]
     public AudioSource audioSource;
     
     [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
     public bool playOnAwake = false;
 
     [Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
     public bool loop = false;
 
     private bool soundsGoodToPlay = false;
     
     void Start ()
     {
         // make sure we have a reference to the AudioSource
         if (audioSource == null)
         {
             if (audioSource == null)
             {
                 audioSource = gameObject.AddComponent<AudioSource>();
             }
             else
             {
                 audioSource = gameObject.GetComponent<AudioSource>();
             }
         }
         audioSource.playOnAwake = playOnAwake;
         audioSource.loop = loop;
 
         // this loop just make sure that alll the AudioClips in the soundList are valid
         for (int i = (soundList.Count - 1); i >= 0; i--)
         {
             if (soundList[i] == null)
             {
                 soundList.RemoveAt(i);
             }
         }
     }
 
     void PlaySequentialSounds()
     {
         StartCoroutine(PlaySounds());
     }
 
     IEnumerator PlaySounds()
     {
         if (soundList.Count > 0)
         {
             for (int i = 0; i < soundList.Count; i++)
             {
                 audioSource.PlayOneShot(soundList[i]);
 
                 // wait for the lenght of the clip to finish playing
                 yield return new WaitForSeconds(soundList[i].length);
             }
         }
         yield return null;
     }
 }
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 rachmadrin · Jul 18, 2016 at 08:16 AM 0
Share

Thanks. :D Because i want put my command voice in my game, and i divide it into several part. But, when i put it all those things into main camera, its play on the same time. The only way i know to put audio in my game is by put it in main camera. If you have better method, it'll be my pleasure to know it. Thank you very much.

avatar image TBruce rachmadrin · Jul 18, 2016 at 06:27 PM 0
Share

You can have an AudioSource anywhere, it does not have to be on the $$anonymous$$ainCamera. Because you can only have one AudioListener per scene is the reason why the AudioListener is on the main camera.

You can attach either of the classes above to a separate GameObject. I usually have a GameObject called "Game $$anonymous$$anager" in my game where I place all my game management components (or I make child GameObjects) "e.g. Game Object $$anonymous$$anager, Audio $$anonymous$$anager..."

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

73 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

Related Questions

my audiosource does not play after gameobject falling 0 Answers

Play a variety of audio source objects from a for loop 2 Answers

Playing and Pausing multiple audio clips 1 Answer

Audio Clipping Problem 0 Answers

3D audio sources Harsh Panning problem 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