Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Jammer3000 · Dec 03, 2013 at 07:48 PM · erroraudio

How to play multiple audio clips in a row?

Hi here's what I want to do. I have a scene where I want my script to loop through multiply audio clips in a row that I assign via the inspector. I'm currently using this script(Script1) to play the audio clip thats assigned to the audio source attached to my game object and then assign the next audio clip once the first one ends and Im using the yield WaitForSeconds to make sure it plays through the entire audio clip before assigning the new one to the audio source. But this isn't what I want so I modified the script(Script2) so that it will loop through 3 audio clips, but it won't work at all and the only error that displays is this one: Script error (audio): Update() can not be a coroutine. Please help! Thanks (:

Script1

 #pragma strict
 @script RequireComponent(AudioSource)
 
 public var soundClip1 : AudioClip;
 
     // Play default sound
     function Start()
     {
             audio.Play();
             // Wait for the audio to have finished
             yield WaitForSeconds (audio.clip.length);
             // Assign the other clip and play its
             audio.clip = soundClip1;
             audio.Play();
     }



Script2

 #pragma strict
 @script RequireComponent(AudioSource)
 
 public var soundClip1 : AudioClip;
 public var soundClip2 : AudioClip;
 public var soundClip3 : AudioClip;
     // Play default sound
     function Update ()
     {
     
         if(Input.GetKey("m")) {
             audio.Play();
             // Wait for the audio to have finished
             yield WaitForSeconds (audio.clip.length);
             // Assign the other clip and play its
             audio.clip = soundClip1;
             audio.Play();
             
         }
         else if(Input.GetKey("m")) {
         
             audio.clip = soundClip2;
             audio.Play();
             yield WaitForSeconds (audio.clip.length);
             
         }
     }
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
2
Best Answer

Answer by KiraSensei · Dec 03, 2013 at 08:02 PM

"Update() can not be a coroutine" means that you cannot call WaitForSeconds into the Update() method.

So I suggest you to do the following code if you want to keep the same logic :

 #pragma strict
 @script RequireComponent(AudioSource)
  
 public var soundClips : AudioClip[3];
 
 private var musicNumber:int = 0;
 private var playNextMusic:boolean = true;
     
 // Play default sound
 function Update ()
 {
     if(Input.GetKey("m")) {
         if (playNextMusic) PlayTheNextMusic();
     }
 }
 
 function PlayTheNextMusic() {
     playNextMusic = false;
     audio.clip = soundClips[musicNumber];
     audio.Play();
     yield WaitForSeconds (audio.clip.length);
     playNextMusic = true;
     ++musicNumber;
     if (musicNumber == AudioClip.Length) musicNumber = 0; // I'm not sure if it is Length or length here...
 }
Comment
Add comment · Show 13 · 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 Jammer3000 · Dec 04, 2013 at 03:58 AM 0
Share

okay I pasted your code in and commented out $$anonymous$$e but now its telling me its expecting a semicolon on line 9 and 34 spaces over which is after the AudioClip[3]; array your making?

avatar image KiraSensei · Dec 04, 2013 at 07:46 AM 0
Share

Oh sure, I didn't see my mistake :

 public var soundClips:AudioClip[] = new AudioClip[3];
avatar image Jammer3000 · Dec 05, 2013 at 11:12 PM 0
Share

Can I make this loop through as many audio clips as I want?

avatar image Jammer3000 · Dec 05, 2013 at 11:22 PM 0
Share

Okay thanks it works great with switching through the clips but is there anyway that once the m key is clicked it just loops through all the audio clips but it would be cool if it still switched to the next one when the user clicked the m key but it doesn't just stop when the clip is over it actually moves on to the next audio clip and so on?

avatar image KiraSensei · Dec 05, 2013 at 11:23 PM 0
Share

Of course, you just have to create your array as long as you need, or use lists and add them one by one. Here the code is for 3 but you can put 10, 100 audio clips if you want.

You may also use the Random function to randomly play them.

Show more comments
avatar image
0

Answer by twobob · Nov 21, 2014 at 02:15 PM

"I don't know why the code above would not work."

  if (musicNumber == AudioClip.Length) musicNumber = 0;


should be

  if (musicNumber == soundClips.Length) musicNumber = 0;


would be my guess at first glance.

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

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

18 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

Related Questions

How to emit a sound on specific collision. 1 Answer

Access components in ALL children 1 Answer

SetActive() Object reference not set to an instance of an object. 1 Answer

How to loop a sound clip on a ButtonDown function 1 Answer

Scripting error #2! 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