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 MiloMan · Jan 18, 2017 at 09:28 PM · audioaudiosourceaudioclip

How to play only one sound at the same time ?

Im creating a soundboard game and i want to play only one sound at the same time. When I press another button I want to stop the first sound and play second sound. What should I do ? Sorry for my English :/

using UnityEngine; using System.Collections; using UnityEngine.UI;

 [RequireComponent(typeof(Button))]
 public class ClickSound : MonoBehaviour {
 
 
     public AudioClip sound;
 
     private Button button { get { return GetComponent<Button> (); } }
     private AudioSource source { get { return GetComponent<AudioSource> (); } }
 
 
 
 
     void Start ()
     {
         gameObject.AddComponent<AudioSource> ();
         source.clip = sound;
         source.playOnAwake = false;
 
 
         button.onClick.AddListener (() => PlaySound ());
     }
 
     void PlaySound ()
     {
         source.PlayOneShot (sound);
     }
 }
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

1 Reply

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

Answer by HenryStrattonFW · Jan 18, 2017 at 09:32 PM

All you need to do is swap "PlayOneShot" for "Play", and also it's probably best to put a source.Stop() call just before you set the clip on the source, so basically the logic flows as such:

  • Stop sound currently playing.

  • Change to new sound.

  • Play new sound.

EDIT: Had another read over your code and realised I missed the point a bit first time around (sorry about that) I'm assuming that this component you've written is on all of your buttons. In that case I would suggest you store the currently playing button in a static variable when you play a sound. That way when you try to press a new button, you can check this value, ensure the sound on it is stopped, and then play your new sound before then setting that button as the last active button. Here's some code that should do the trick.

     private static ClickSound lastClickSound;
 
     void PlaySound()
     {
         if (lastClickSound != null && lastClickSound.source != null)
         {
             lastClickSound.source.Stop();
         }
         source.Play();
         lastClickSound = this;
     }
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 MiloMan · Jan 19, 2017 at 06:52 PM 0
Share

Thank you <3

avatar image HenryStrattonFW MiloMan · Jan 19, 2017 at 08:23 PM 0
Share

You're welcome. good luck with your soundboard 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

102 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 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 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 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 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 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 make audio play at once. 1 Answer

Cracking at end of audio? 0 Answers

Audio not playing when destroying a GameObject. 1 Answer

Need help: new sound isnt played correctly anymore 2 Answers

Don't reload the AudioSource when back to scene 1 Answer


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