• 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
2
Question by Earless · Oct 13, 2014 at 02:53 PM · audioaudiosourceaudioclip

No overlapping sounds

Hey guys,

quick question (I hope). How do I stop audio clips from overlapping? I'm creating a Pacman game, and when Pacman eats a candy, I want a sound to play. However, if an eating sound is still playing, I dont want the new sound to start. Only if one eating sounds is completed, and Pacman eats another candy, I want the clip to play again. Partial piece of my code for this:

 public AudioClip candy;
 
 void OnTriggerEnter(Collider other) 
     {
         if (!pause)
         {
             if (other.gameObject.tag == "PickUp")
             {
                 other.gameObject.SetActive(false);
                 if (!audio.isPlaying)
                     AudioSource.PlayClipAtPoint(candy, transform.position);
                 count = count + 1;
                 score = score + 10;
                 //scoreText.text = "Score: " + score;
                 if (count >= totalPickupCount)
                 {
                     if (!fail)
                     {
                         timeScore = Mathf.FloorToInt(10 * startTimer);
                         liveScore = Lives.lives * 100;
                     }
                     storeEndScore();
                     storeEndTimeScore();
                     storeEndLiveScore();
                     Application.LoadLevel("Score"); //Game is klaar - ga naar Score Scherm
             
                 }
             }
       }

As you can see, i treid using audio.isplaying. However this has no effect. Any other ideas/options?

Comment
Add comment · Show 10
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 SaraCecilia · Oct 13, 2014 at 01:51 PM 0
Share

Hi, please check your code once more. Like adding curly brackets around: AudioSource.PlayClipAtPoint(candy, transform.position); count = count + 1; score = score + 10;

avatar image Earless · Oct 13, 2014 at 09:50 PM 0
Share

Is not needed, since count = count + 1 shouldnt be in the if statement :)

avatar image KpjComp · Oct 13, 2014 at 10:29 PM 0
Share

audio, and AudioSource.PlayClipAtPoint I believe don't use the same AudioSource, and I think isPlaying is a flag for if you have it paused anyway.. But if you use your own AudioSource ins$$anonymous$$d, you can check what's playing by checking it's output data and see if it's playing anything. Here is a function that does it.

     bool isPlaying(AudioSource p) {
         float[] samples = new float[1024];
         audio.GetOutputData(samples, 0);
         for(int l = 0; l < 1024; l ++) {
             if (samples[l] != 0) return true;
         }
         return false;
     }

Alternatively you could also set a flag to say if your playing audio, to unset the flag check the length of the audio, and with a timer unset after that length.

avatar image MrSoad · Oct 13, 2014 at 10:41 PM 0
Share

I think $$anonymous$$pjComp is on the right track here. The :

 if (!audio.isPlaying) {

Will only check if the Audio Source that is on the same object that the script is on is Playing. If you want to check for a playing audio source on another object you need to reference that object like :

 if (!$$anonymous$$y_Other_GameObject.audio.isPlaying) {

I can't comment on the play clip at point, never used it sorry.

avatar image KpjComp · Oct 13, 2014 at 10:52 PM 0
Share

$$anonymous$$rSoad you a right here, the docs for isPlaying I would say is wrong though. >> AudioSource.isPlaying will return false when AudioSource.Pause() is called.. isPlaying does indeed return false when audio has stopped playing too, not just when pause is called. So my isPlaying function is not needed. Just using an AudioSource ins$$anonymous$$d of PlayClipAtPoint does the trick.

avatar image KpjComp · Oct 13, 2014 at 10:58 PM 0
Share

Earless, To add is there any reason you wouldn't want 2 audio's playing at the same time?.. As that can also be done.

avatar image Earless · Oct 14, 2014 at 10:20 AM 0
Share

@$$anonymous$$pjComp It's just that it's annoying when 4 of the same sounds are playing simultaneously. If I pickup an object I want the sound to be played once, not 4 times. Right now Im using audio clip. Is it better to attach every audio clip to an audio source?

avatar image MrSoad · Oct 14, 2014 at 11:04 AM 0
Share

I tend to have one Audio Source for every clip. It stops you having to not only switch the clip with scripting but normally the settings for a clip are different meaning you have to script all the setting changes as well. If I need a lot of sounds on one object I parent appropriately named empty objects to this object and put Audio sources on these to reference and play when needed. This also means you can play these sounds at the same time, which you can't if you swap. Though in your case you are trying to stop multiple sounds playing...

avatar image Earless · Oct 15, 2014 at 07:07 AM 0
Share

@$$anonymous$$rSoad I turned away from using the clips. I'm also using AudioSources now. While it's still not perfect, at least the sounds aren't overlapping, but more like cancelling each other.

avatar image MrSoad · Oct 15, 2014 at 11:31 AM 0
Share

Cancelling can happen for two reasons that I know of :

1) The Audio Source is playing already and is being told to play again, which can make it start playing from the start again mid way through its current play.

2) All your audio channels are full, so to play a new sound it needs to empty out a currently playing one. This is where the priority settings come into affect. The current lowest priority audio source will cut out if one with a higher priority is told to play when this happens.

Note : That 0 is the highest priority(always override) and 255 is the lowest priority.

Hope this helps.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio loops too early 2 Answers

detect audio then record and then play 0 Answers

Audio clip not playing 1 Answer

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

AudioClip.Create() + PlayClipAtPoint() Not Working 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