This question was
closed Dec 05, 2016 at 04:57 AM by
GamerHarp for the following reason:
I found my mistake
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
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());
}
}