• 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 omarR · Jul 28, 2013 at 02:48 PM · audioaudiosourceaudioplayplayoneshot

Audio.PlayOneShot problem

Hello everyone I am working on a project, simple fps. I have a zombie enemy roaming and I want it to PlayOneShot when I am detected, the problem is when I get close and it detects me the audio plays in stacks .. like 50 times together if you know what I mean.. any suggestions ? thank you

 public AudioClip Dtct;
 
 void Update () {
 
  if(Vector3.Distance(player.position, transform.position)<distance){
         audio.PlayOneShot(Dtct);
        
  }
 }
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
1
Best Answer

Answer by Orlando Bascunan · Jul 28, 2013 at 03:03 PM

Hola! You should make a bool storing if its already played like this

 public AudioClip Dtct;
 private bool soundPlayed = false;
 void Update () {
  
  if(!soundPlayed){
       if(Vector3.Distance(player.position, transform.position)<distance){
            audio.PlayOneShot(Dtct);
            soundPlayed = true;
         }
     }
 }
Comment
Add comment · Show 1 · 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 omarR · Jul 28, 2013 at 03:10 PM 1
Share

thank you very much .. that fixed it. :)

avatar image
1

Answer by dan19 · Jul 28, 2013 at 02:55 PM

This happens because once you approach the zombie, you start calling PlayOneShot() every frame, and it starts playing the sound many times a second. You should somehow prevent subsequent calls. Try this:

 if(Vector3.Distance(...) < distance && !audio.isPlaying) {
     audio.PlayOneShot(Dtct);
 }
Comment
Add comment · Show 5 · 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 omarR · Jul 28, 2013 at 02:59 PM 0
Share

Thank you for the quick reply, but this will not work since it is checking if audio is playing, so once it does it plays the audio and the same problem happens x[

avatar image dan19 · Jul 28, 2013 at 03:07 PM 1
Share

It will start playing again only when the previous sound stops playing. Did you notice the boolean negation (!) ?

But it will start playing again once it is done if you stay near the zombie. If you don't want that' you'll have to use some additional state to remember that you don't want to play the sound any more. Like this, for instance:

 private bool nearZombie = false;
 public void Update() {
     if(Vector3.Distance(...) < distance) {
         if(!nearZombie && !audio.isPlaying) {
             audio.PlayOneShot(...);
         }
         nearZombie = true;
     } else { 
         nearZombie = false;
     }
 }
avatar image dan19 · Jul 28, 2013 at 03:08 PM 0
Share

Although I haven't checked whether or not isPlaying is affected by PlayOneShot(). It is definitely affected by Play() though.

avatar image omarR · Jul 28, 2013 at 03:12 PM 0
Share

I appreciate your effort, the first code you wrote gave me the same problem I was having. I used Orlando's advice and it got it fixed, which is similar to the code you wrote just now, thank you very much again :))

avatar image dan19 · Jul 28, 2013 at 03:15 PM 0
Share

Yep, this page says that isPlaying is not affected by PlayOneShot(). So you should replace audio.PlayOneShot(...) with something like

 audio.clip = Dtct;
 audio.Play();
avatar image
0

Answer by LaurynasLubys · Apr 04, 2018 at 11:09 AM

I experienced this problem as well, but I understood it as if I have too many SoundSources on objects (total amount of sound sources in the scene).

My setup was that I had a new sound source per sound clip. For example object would have 5 sound clips, therefore script would create 5 sound sources and parent to the object.

Instead, I modified my script to switch sound clips on random to the same sound source. So that one object would always have a single sound source. Perhaps it is intended like that in a first place. Anyway, now all sounds are played as they should, and I don't get this error. One problem with that is that code needs to reassign sound clip to the sound source each time when new sound should play.alt text


single-sound-source.jpg (436.0 kB)
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers

AudioSource plays noisy audio 1 Answer

Stop audio from looping and play at lower volume 1 Answer

Play local Android/IOS audio files in unity game. 0 Answers

Problem with GetSpectrumData 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