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 · Nov 20, 2012 at 04:49 PM · audioaudioclip

How to play several audio cllips one after another.

Hi, heres what i'm trying to do this script I have here is great, but it only plays one audio clip(Clip1) after the first one, I need it to play ten one after another, and then once it gets through them all restarts and goes again,thanks.

var Clip1: AudioClip; var playNow = false;

yield WaitForSeconds(audio.clip.length); playNow = true;

function Update () {

if(playNow) { // Assign Clip1 and play it audio.clip = Clip1; audio.Play(); playNow = false; } }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Olgo · Nov 20, 2012 at 06:34 PM

Create an array of audio clips, create a for loop inside your if(playNow) statement. At the of the for loop, yield for the length of the audio clip, then increment the counter to go to the next item in the array.

Comment
Add comment · Show 2 · 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 · Nov 20, 2012 at 06:54 PM 0
Share

Thanks, but I am new to code and I would be able to do that, sorry if there's anyway you can write the code for me that would be great, so sorry again.

avatar image Olgo · Nov 20, 2012 at 09:16 PM 0
Share

I'm pretty new myself, but I like to use UnityAnswers for learning exercises. That said, I think I was able to put something together that will work for you. I'll post it in a new answer so we can have the nice formatting.

avatar image
0

Answer by Olgo · Nov 20, 2012 at 09:19 PM

Paste this into PlaySoundArray.cs, add it to a game object with an audio source. Drag the sound clips into the array once you have specified the length.

 using UnityEngine; 
 using System.Collections;
 
 [RequireComponent (typeof (AudioSource))]
 public class PlaySoundArray : MonoBehaviour {
     
     public float hSliderValue = 1.0f;
     public AudioClip[] clips;
     public int hideGUItime = 5;
     
     private bool GUIhidden = false;
     private float mouseMoveTime = 0f;
     private bool playNow = false;
     private int cnt = 0;
     private Vector3 tmpMousePos;
 
     void Update () {
         DetectMouseMov();
         
         mouseMoveTime = mouseMoveTime + Time.deltaTime;
         
         if(mouseMoveTime > hideGUItime)
             GUIhidden = true;
         else
             GUIhidden = false;
         
         if(Input.GetKeyUp(KeyCode.M)){
                 playNow = !playNow;
                 audio.Stop();
         }
             
         if(playNow){
          PlaySounds();
            } 
     }
 
     void PlaySounds(){
         if(!audio.isPlaying && cnt < clips.Length){
               audio.clip = clips[cnt];
               audio.volume = hSliderValue;
             audio.Play();
               cnt = cnt + 1;
          }
     if(cnt == clips.Length)
       cnt = 0;
     }
     
     void OnGUI() {
         if(!GUIhidden){
             GUI.Label(new Rect(25, Screen.height - 50, 100, 30), "Volume");
             hSliderValue = GUI.HorizontalSlider(new Rect(25, Screen.height - 25, 100, 30), hSliderValue, 0.0f, 1.0f);
         }
         Screen.showCursor = !GUIhidden;
     }
     
     void DetectMouseMov(){
         if (tmpMousePos != Input.mousePosition){
             mouseMoveTime = 0;
             tmpMousePos = Input.mousePosition;
         }
     }
 }
Comment
Add comment · Show 14 · 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 · Nov 21, 2012 at 03:12 PM 0
Share

Thanks Olgo, but when I assign it to my main camera, and add two or more audio clips just like you said, nothing happens and there's no errors or anything either.

avatar image Olgo · Nov 21, 2012 at 03:25 PM 0
Share

Blast! I wasn't able to test it when I wrote it because I was at work. Still am, but I'm being sneakier. Looks like we never make it into the IEnumerator PlaySounds function. However, I change IEnumerator to void and it plays just fine, but we don't get to use the nifty yield command to wait for the end of the sound. Gimme a couple $$anonymous$$utes and I'll have it fixed. I have an idea.

avatar image Olgo · Nov 21, 2012 at 03:55 PM 0
Share

Just to keep things cleaner around here. I got rid of the old script and updated this original answer to contain the working one.

This will cycle through your array of sounds as long as playNow is true. It will stop when playNow is false, and pick up where it left off when playNow is true again.

avatar image Jammer3000 · Nov 21, 2012 at 07:49 PM 0
Share

Thanks Olgo it works great, now if you could make it so when the player clicks the "m" key on the keyboard then it plays the audio clips and if the click it again it pauses the audio clips, ins$$anonymous$$d of it just doing it on start, I could probably do it, but I only know Java script and Im not good at converting code, if you don't, have time thats fine Thanks again Olgo.

avatar image Olgo · Nov 21, 2012 at 11:55 PM 0
Share

No problem, I'll update the answer again with the latest changes. I've tested it, the $$anonymous$$ key starts and stops the cycle now. We have version 3.0 :) maybe more like 2.1

Show more comments
avatar image
0

Answer by Taimoor_Ahmed · Aug 11, 2017 at 02:13 PM

Hey @Jammer3000; Solution to your problem is;

   // declare Variable
         private bool OnPauseMenuSong = false;
 void Update(){
  if (playNow)
     {
     PlaySounds();
     OnPauseMenuSong = !OnPauseMenuSong;

     if(OnPauseMenuSong == true){
     PlaySound();
     }
  }



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

12 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

Related Questions

How to play an audio one shot clip on a collision 1 Answer

C# Script Template - how to make custom changes? 1 Answer

Unity Unable to Reassign Audio Clip 1 Answer

Does anyone know of good C sharp tutorials? 0 Answers

Why my sound it's not attaching to my Flashlight 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