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 moodooguru123 · Aug 08, 2016 at 07:16 PM · audioenemyaudiosourceplaynew user

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 alt text

 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 ();
     }


screen-shot-2016-08-08-at-112112-am.png (86.6 kB)
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

2 Replies

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

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.

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 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;
     }
 }
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 TBruce · Aug 08, 2016 at 08:26 PM 0
Share

I am glad that this works for you.

Good luck on your project.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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