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 Entyro · Sep 16, 2013 at 02:42 PM · audiosound

Wait for sound is played

Hi!

I was wondering how you could play a sound, but only after the first sound is already played. For example, when i press the key "1" a sound plays, then when I press key "2" a second sound needs to play, but only if the first one is done playing.

Does anyone know how to do that?

 var sound1: AudioClip;
 var sound2: AudioClip;
 var sound3: AudioClip;
 var sound4: AudioClip;
 var sound5: AudioClip;
 
 function Start () {
 
 }
 
 
 function Update () {
 
     if (Input.GetKeyDown ("1")&& !audio.isPlaying)
         {
             AudioSource.PlayClipAtPoint(sound1, transform.position);
     }
     
     if (Input.GetKeyDown ("2")&& !audio.isPlaying)
         {
             AudioSource.PlayClipAtPoint(sound2, transform.position);
     }
     
     if (Input.GetKeyDown ("3")&& !audio.isPlaying)
         {
             AudioSource.PlayClipAtPoint(sound3, transform.position);
     }
     
     if (Input.GetKeyDown ("4")&& !audio.isPlaying)
         {
             AudioSource.PlayClipAtPoint(sound4, transform.position);
     }
     
     if (Input.GetKeyDown ("5")&& !audio.isPlaying)
         {
             AudioSource.PlayClipAtPoint(sound5, transform.position);
     }
 }
Comment
Add comment · Show 5
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 vexe · Sep 16, 2013 at 03:02 PM 0
Share

Do you want them to stack? (ie press 1, play clip 1. then press 2, 3, 4 to play 2 after 1 finishes, 3 after 2 finishes, etc)

avatar image Entyro · Sep 16, 2013 at 03:06 PM 0
Share

No I wanna press them randomly.

avatar image vexe · Sep 16, 2013 at 03:07 PM 0
Share

Yes, but you do want them to stack and play one after the other no matter how many you press right?

avatar image Entyro · Sep 16, 2013 at 03:16 PM 0
Share

I don't think I want to stack them. Only able to play a sound when there's no other playing

avatar image vexe · Sep 16, 2013 at 03:20 PM 0
Share

I got what you mean, just a $$anonymous$$ and you'll get your script right up ;)

2 Replies

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

Answer by vexe · Sep 16, 2013 at 03:45 PM

For that, don't use AudioSource.PlayClipAtPoint as this will instantiate a new AudioSource at the position and clip you specify via its input arguments, and plays that clip at that position, when the clip is done playing the AudioSource that got instantiated will get destroyed.

So simply, attach an AudioSource to your gameObject, and play the clips normally with audio.Play.

Here's what I just came up for you (tested), it's a bit hacky, but really cool: The basic idea behind this script, is to use a list of audio clips, each time you press a number, like 1, 2, 3 etc the sound equivalent to that number will get added to the end of the list. Then, we simply play the first clip from the list (at index 0), we don't do anything till we finish playing that clip, and when you do we remove it from our list. And since it was at index 0, the next clip after it will take its place, so we can still keep playing at the same index :) - The variable hasJustStartedPlaying (just a boolean flag) is very important key for this hack to work properly. See the script to understand the logic.

 import System.Collections.Generic;

 private var clipsToPlay : List.<AudioClip> = new List.<AudioClip>();
 private var hasJustStartedPlaying : boolean;
 
 function Update()
 {
      if (Input.GetKeyDown ("1")) {
         clipsToPlay.Add(sound1);
      }
      else if (Input.GetKeyDown ("2")) {
         clipsToPlay.Add(sound2);
      }
      else if ....

      PlayClips();
 }


 function PlayClips()
 {
     // if there's no audio playing, and we haven't started playing yet
     if (!audio.isPlaying && !hasJustStartedPlaying) {
         if (clipsToPlay.Count > 0) {
             hasJustStartedPlaying = true;
             audio.clip = clipsToPlay[0]; // we'll always play the first clip, and when we're done with it, we'll nuke it
             audio.Play();
         }
     }

     // keep skipping frames while the clip is playing
     while (audio.isPlaying)
         yield;

     // when we reach this point, we're done playing the clip, so we'll nuke it from the list
     // why do we need to check for `hasJustStartedPlaying`? well, remove it and see what happens, and try to guess why it happened...
     if (clipsToPlay.Count > 0 && hasJustStartedPlaying)
     {
         clipsToPlay.RemoveAt(0);
         hasJustStartedPlaying = false;
     }
 }

Btw using a Queue structure instead of a List in this situation is more suitable, but for simplicity's sake, I used a list.

EDIT: After reading your comment

"I want it so when you press 1 a sound plays, then if you press 2, 3, 4 etc nothing happens. Not even after the first sound is done. I just want to be able to play a sound when there's no other sound playing. So I don't want the sounds to stack on each other."

The solution becomes rather much simpler:

 function Update()
 {
    var clip2Play : AudioClip = null;
    if (Input.GetKeyDown("1") {
       clip2Play = sound1;
    }
    else if (Input.GetKeyDown("2") {
       clip2Play = sound2;
    }
    else if ...
 
    if (!audio.isPlaying && clip2Play != null) {
       audio.clip = clip2Play;
       audio.Play();
    }
 }
Comment
Add comment · Show 6 · 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 vexe · Sep 16, 2013 at 03:54 PM 0
Share

Let me know how it goes... If there's something you didn't understand, please say so...

avatar image Entyro · Sep 16, 2013 at 05:48 PM 0
Share

There was a problem with the "else if..." so I added just "else". The problem I have now is that the sound repeats and because of that you can't change sound from for example 1 to 2

avatar image vexe · Sep 16, 2013 at 05:56 PM 0
Share

What do you mean the sound repeats?

$$anonymous$$y script works like follows:

 press 1, 3, 1 (play sound 1, when finished play 3, when finished play 1)
 
 press 1, 1, 1 (play sound 1 three times, one after the other)
 
 press 2, 3, 3 (play 2, then 3, then 3)

If you use only if ins$$anonymous$$d of if - else this means that you could press a bunch of numbers at the same time together. using if-else will insure that only one key is pressed.

I have to go now, be back after a while. $$anonymous$$eep giving feedback if you're still having trouble.

avatar image Entyro · Sep 16, 2013 at 07:03 PM 0
Share

Aha now I understand. I want it so when you press 1 a sound plays, then if you press 2, 3, 4 etc nothing happens. Not even after the first sound is done. I just want to be able to play a sound when there's no other sound playing. So I don't want the sounds to stack on each other.

avatar image vexe · Sep 17, 2013 at 03:39 AM 0
Share

That is rather simple. Answer updated.

Show more comments
avatar image
0

Answer by chelder · Sep 09, 2015 at 06:58 PM

In Unity 5 I'm using the PlayDelayed(float seconds) function together with isPlaying as follows:

     if (soundToWait.isPlaying) {
         nextSound.PlayDelayed (soundToWait.clip.length);
     } else {
         nextSound.Play();
     }

It is not perfect though. The ideal would be that instead using soundToWait.clip.length, we could do something like "soundToWait.timeLeftToFinishSound"... Maybe it is possible. I did not research it deeply!

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Stop another sound from another script? 1 Answer

Get acess and switch to many audio source in a single Object HELP!! 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Gun Audio reverbs off of Terrian- sounds horrible- What can I do? 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