Play a variety of audio source objects from a for loop
Hi, I'm trying to make a simple kids counting game which displays each number (1-100) with the corresponding audio. In my script, I made public Audio Source variables and in the Inspector I linked the Audio Source objects which contain the correct audio files.
Here's the problem: Right now both of my sounds play at the same time. So user will hear audio for 1 and 2 simultaneously. Instead I want the sounds to play one after another while the corresponding number is displayed on screen.
I'm new to Unity and your help is appreciated! Thank you
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class GameController_Count : MonoBehaviour {
public Text textNumber;
public Text textNumberAsWords;
public AudioSource a1;
public AudioSource a2;
IEnumerator CountUp () {
for (int textNum = 1; textNum < 100; textNum++) {
print (textNum);
textNumber.text = textNum.ToString ();
if (textNum == 1) {
textNumberAsWords.text = "one";
a1.Play ();
yield return new WaitForSeconds (2.0f);
}
if (textNum == 2) {
textNumberAsWords.text = "two";
a2.Play ();
yield return new WaitForSeconds (2.0f);
}
}
}
void Start () {
CountUp ();
}
Answer by TBruce · Aug 08, 2016 at 07:20 PM
You current code will only play the sounds 1 and 2, Assuming that you want to play sounds 1 - 100 you can do this
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(AudioSource))]
public class GameController_Count : MonoBehaviour
{
public Text textNumber;
public Text textNumberAsWords;
public AudioSource audioSource1;
public AudioSource audioSource2;
public List<AudioClip> numberOfClips = new List<AudioClip>();
public int maxSounds = 100;
private string[] numbersAsText = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Telve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen", "Twenty",
"Twenty One", "Twenty Two", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
"Thirty One", "Thirty Two", "Thirty Three", "Thirty Four", "Thirty Five", "Thirty Six", "Thirty Seven", "Thirty Eight", "Thirty Nine", "Fourty",
"Fourty One", "Fourty Two", "Fourty Three", "Fourty Four", "Fourty Five", "Fourty Six", "Fourty Seven", "Fourty Eight", "Fourty Nine", "Fifty",
"Fifty One", "Fifty Two", "Fifty Three", "Fifty Four", "Fifty Five", "Fifty Six", "Fifty Seven", "Fifty Eight", "Fifty Nine", "Sixty",
"Sixty One", "Sixty Two", "Sixty Three", "Sixty Four", "Sixty Five", "Sixty Six", "Sixty Seven", "Sixty Eight", "Sixty Nine", "Seventy",
"Seventy One", "Seventy Two", "Seventy Three", "Seventy Four", "Seventy Five", "Seventy Six", "Seventy Seven", "Seventy Eight", "Seventy Nine", "Eighty",
"Eighty One", "Eighty Two", "Eighty Three", "Eighty Four", "Eighty Five", "Eighty Six", "Eighty Seven", "Eighty Eight", "Eighty Nine", "Ninety",
"Ninety One", "Ninety Two", "Ninety Three", "Ninety Four", "Ninety Five", "Ninety Six", "Ninety Seven", "Ninety Eight", "Ninety Nine", "One Hundred"};
void Start ()
{
// make sure that there are audio clips
if (numberOfClips.Count > 0)
{
maxSounds = numbersAsText
// limit the number of sounds
int count = numberOfClips.Count;
if (count > maxSounds)
{
count = maxSounds;
}
StartCoroutine(CountUp());
}
}
IEnumerator CountUp ()
{
for (int textNum = 0; textNum < count; textNum++)
{
print (textNum);
textNumber.text = (textNum + 1).ToString();
textNumberAsWords.text = numbersAsText[i];
// textNum is an odd number and audioSource2 is not null play clip on audioSource2
// otherwise play clip on audioSource1
if (((textNum % 2) == 1) && (audioSource2 != null))
{
audioSource2.PlayOneShot(numberOfClips[i]);
}
else
{
audioSource1.PlayOneShot(numberOfClips[i]);
}
// wait the length of the clip before continuing on to the next clip
yield return new WaitForSeconds(numberOfClips[i].length + 0.1f);
}
yield return 0;
}
}
The only thing you need to do is add 100 audio clips to numberOfClips in the inspector.
Also, personally I would only use one AudioSource but you may have a reason for using two. This code however allows you to play all audio clips with either one ore two AudioSource variables.
Answer by moodooguru123 · Aug 08, 2016 at 08:15 PM
@Mavina Thank you so much! I modified your suggestions a bit so it would build successfully for me. Here's the code I arrived at based on your suggestions. As you said, I added the sounds to the GameManager object in the Inspector. Then I just needed to create an empty Audio Source and add that to the GameManager object in the inspector. Works perfectly thank you for your help!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(AudioSource))]
public class GameController_Count : MonoBehaviour
{
public Text textNumber;
public Text textNumberAsWords;
public AudioSource audioSource1;
public List<AudioClip> numberOfClips = new List<AudioClip>();
public int maxSounds = 99;
private string[] numbersAsText = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", "Nineteen", "Twenty",
"Twenty One", "Twenty Two", "Twenty Three", "Twenty Four", "Twenty Five", "Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine", "Thirty",
"Thirty One", "Thirty Two", "Thirty Three", "Thirty Four", "Thirty Five", "Thirty Six", "Thirty Seven", "Thirty Eight", "Thirty Nine", "Fourty",
"Fourty One", "Fourty Two", "Fourty Three", "Fourty Four", "Fourty Five", "Fourty Six", "Fourty Seven", "Fourty Eight", "Fourty Nine", "Fifty",
"Fifty One", "Fifty Two", "Fifty Three", "Fifty Four", "Fifty Five", "Fifty Six", "Fifty Seven", "Fifty Eight", "Fifty Nine", "Sixty",
"Sixty One", "Sixty Two", "Sixty Three", "Sixty Four", "Sixty Five", "Sixty Six", "Sixty Seven", "Sixty Eight", "Sixty Nine", "Seventy",
"Seventy One", "Seventy Two", "Seventy Three", "Seventy Four", "Seventy Five", "Seventy Six", "Seventy Seven", "Seventy Eight", "Seventy Nine", "Eighty",
"Eighty One", "Eighty Two", "Eighty Three", "Eighty Four", "Eighty Five", "Eighty Six", "Eighty Seven", "Eighty Eight", "Eighty Nine", "Ninety",
"Ninety One", "Ninety Two", "Ninety Three", "Ninety Four", "Ninety Five", "Ninety Six", "Ninety Seven", "Ninety Eight", "Ninety Nine"};
void Start ()
{
// make sure that there are audio clips
if (numberOfClips.Count > 0)
{
// limit the number of sounds
int count = numberOfClips.Count;
if (count > maxSounds)
{
count = maxSounds;
}
StartCoroutine(CountUp());
}
}
IEnumerator CountUp ()
{
for (int textNum = 0; textNum < maxSounds; textNum++)
{
print (textNum);
textNumber.text = (textNum + 1).ToString();
textNumberAsWords.text = numbersAsText[textNum];
// otherwise play clip on audioSource1
{
audioSource1.PlayOneShot(numberOfClips[textNum]);
}
// wait the length of the clip before continuing on to the next clip
yield return new WaitForSeconds(numberOfClips[textNum].length + 0.1f);
}
yield return 0;
}
}
I am glad that this works for you.
Good luck on your project.
Your answer
![](https://koobas.hobune.stream/wayback/20220612090417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
audio.play does not work 1 Answer
audio is not playing during IEnumerator 1 Answer
Need help: new sound isnt played correctly anymore 2 Answers
Play audio once not working 0 Answers
Upon hitting Play, my character creates a new Audio Source Component 0 Answers