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 /
This question was closed Dec 05, 2016 at 04:57 AM by GamerHarp for the following reason:

I found my mistake

avatar image
0
Question by GamerHarp · Dec 05, 2016 at 07:33 AM · sounds

Multiple audio loop wont play in certain order

I have looked through many code to play multiple songs but they dont play in order I added some code to try and fix that making an int with a number and saying if int = 1 dont play 1 again but it doesnt work anyway heres the code in c#...

 public class mainMenu : MonoBehaviour {
     public AudioSource[] sound;
     public int justPlayed;
 
     void Start () {
         sound = GetComponents<AudioSource>();
     }
     
 
     void Update () {
         if (!sound[1].isPlaying && !sound[2].isPlaying && justPlayed != 0 && justPlayed != 1){
             sound [0].Play ();
             justPlayed = 0;
         }
         if (!sound[0].isPlaying && !sound[2].isPlaying && justPlayed != 1 && justPlayed != 2){
             sound [1].Play ();
             justPlayed = 1;
         }
         if (sound[0].isPlaying && !sound[1].isPlaying && justPlayed != 2 && justPlayed != 0){
             sound [2].Play ();
             justPlayed = 2;
         }
     }
 }
 
Comment
Add comment · Show 1
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 TBruce · Dec 05, 2016 at 05:26 AM 0
Share

You should consider a different approach. Ins$$anonymous$$d of multiple AudioSource components and if statements use a list of audioClips and coroutine. Though it is not evident how often you want to repat playing the sounds, whether it is once or many times the coroutine can be called again.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class main$$anonymous$$enu : $$anonymous$$onoBehaviour
 {
     [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> audioClips = new List<AudioClip>();
 
     private AudioSource audioSource;
 
     void Start ()
     {
         if (audioClips.Count > 0)
         {
             audioSource = gameObject.GetComponent<AudioSource>();
             if (audioSource == null)
             {
                 audioSource = gameObject.AddComponent<AudioSource>();
             }
             StartCoroutine(PlaySounds());
         }
     }
 
     IEnumerator PlaySounds ()
     {
         for (int i = 0; i < audioClips.Count; i++)
         {
             audioSource.playOneShot(audioClips[i]);
 
             // wait for the lenght of the clip to finish playing
             yield return new WaitForSeconds(audioClips[i].length);
         }
         // uncomment the line below to call coroutine again and
         // start playing from the first clip
         // StartCoroutine(PlaySounds());
     }
 }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Game Sound Recording And Save 0 Answers

i have this problem when i try to shoot sound dont play how do i fix it 0 Answers

Emulating an X-Y oscilloscope? 1 Answer

Players adding images,sounds into the game and save it 0 Answers

Sound Levels vary between levels with same settings? 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