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 madmike6537 · Dec 09, 2012 at 08:03 PM · soundaudiosource

Audio issues.. please help! :)

So I am starting to add some audio to my game, but I am getting some weird results. I have a tree with an audio source, in the audio clip I have an attached clip called tree fall. Now I also have a script on the tree that calls audio.PlayOneShot(treeFall, 1); and in the inspector I have dragged the audioclip into a public variable called treeFall.

Now the clip plays fine but it sounds TERRIBLE. I dont even know what to call it, maybe its playing way to fast, I am just not sure but wow its bad.

The sound file is an MP3.

So questions:

I assume MP3 is fine to use? Do I need to attach the sound to both the audio clip in the audio source component and also call in via a script and public variable? Or would doing this duplicate my sound file maybe?

Thanks much for any help!!

here is a snipit of code:

                     if(Input.GetButtonDown("Fire Weapon"))
                     {
                         startTime = Time.time;
                     }
                     if(Input.GetButton("Fire Weapon"))
                     {
                         if(!audio.isPlaying)
                         {
                         audio.PlayOneShot(chopTree, 1);
                         }
Comment
Add comment · Show 8
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 madmike6537 · Dec 09, 2012 at 08:12 PM 0
Share

Well I found the problem, its in the awake function so its playing it 100 times over. Now to fix :/

avatar image madmike6537 · Dec 09, 2012 at 08:16 PM 0
Share

I think I sort of fixed it by using if(!audio.isPlaying){play audio} but its still playing too many of the audio files, it plays several of them at once. hmph

avatar image AlucardJay · Dec 09, 2012 at 08:42 PM 0
Share

Do you have your AudioSource Component set to Play on Awake? Untick that box! If not, well it is really hard to tell without seeing your code (e.g. is the audio played on Trigger/Collision?). When the issue with the sound occurs, pause the scene (ctrl+shift+P), then check the hierarchy for how many audioClips are playing.

avatar image madmike6537 · Dec 09, 2012 at 08:44 PM 0
Share

Good suggestion - its played on a button down. Ill update the post with the code.

avatar image AlucardJay · Dec 09, 2012 at 09:05 PM 1
Share

O$$anonymous$$, I see the problem. What's happening is audio.PlayOneShot creates an instance much like PlayClipAtPoint, a separate gameObject that plays the audio then destroys itself. So when you ask if(!audio.isPlaying) it will always return false, as that object is not playing sound, a different object is. Simply use audio.Play() , and make sure the Loop box is also unchecked =]

Edit : added as answer

Show more comments

3 Replies

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

Answer by AlucardJay · Dec 10, 2012 at 01:37 AM

OK, I see the problem. What's happening is audio.PlayOneShot creates an instance much like PlayClipAtPoint, a separate gameObject that plays the audio then destroys itself. So when you ask if(!audio.isPlaying) it will always return false, as that object is not playing sound, a different object is. Simply use audio.Play() , and make sure the Loop box is also unchecked =]


Sure =]

Here are the Unity Scripting References :

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Play.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayClipAtPoint.html

http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html

I have several answers I could also link, but for now I think this one will explain how to assign an audioClip in script, change audio clips and check if they are already playing :

http://answers.unity3d.com/questions/347657/play-audioclip-once-and-switching-clips.html

(I'm Jay Kay / alucardj)

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 AlucardJay · Dec 10, 2012 at 01:50 AM 0
Share

Thanks for that, hope the added information helped. Here are some more answers you may find useful :

http://answers.unity3d.com/questions/307478/randomly-play-audio.html

http://answers.unity3d.com/questions/315418/sound-problem-.html

http://answers.unity3d.com/questions/233420/musical-game-mute-and-unmute-trigger-soundsource.html

http://answers.unity3d.com/questions/343383/switch-between-two-different-sounds-for-walking-an.html

avatar image madmike6537 · Dec 10, 2012 at 01:52 AM 0
Share

Very helpful thanks much I will look at those as well!

avatar image
0

Answer by Msurdej · Dec 09, 2012 at 08:45 PM

It might be because it is a 3D sound. This can cause the sound to have a weird doppler effect. Try changing the Sound file in Unity to not be a 3D Sound via the Inspector.

EDIT: You might want to try audio.PlayOneShot then, as that only plays the sound once.

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 madmike6537 · Dec 09, 2012 at 08:59 PM 0
Share

I think because its in update its still playing oneshot multiple times though. not sure whats up with that. the if statement should prevent that.

avatar image AlucardJay · Dec 09, 2012 at 09:06 PM 0
Share

Nope, sorry. The issue is multiple audioClips are playing when the OP only wants one at a time. Also with your edit, the OP originally stated they were already using PlayOneShot :

 audio.PlayOneShot(treeFall, 1);
avatar image
0

Answer by Gori67 · Aug 27, 2014 at 04:15 PM

Thanks this answer was very helpfull for me. Problem solved :)

OK, I see the problem. What's happening is audio.PlayOneShot creates an instance much like PlayClipAtPoint, a separate gameObject that plays the audio then destroys itself. So when you ask if(!audio.isPlaying) it will always return false, as that object is not playing sound, a different object is. Simply use audio.Play() , and make sure the Loop box is also unchecked =]

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

Mac built-in microphone stopped sending data to Unity. 0 Answers

Sample-precise sync on multiple audio sources ? 1 Answer

What to do to hear a voice over only 1 time? 2 Answers

Attach audio source to different part of object 0 Answers

play a sound when/while rigidbody velocity is greater than a value 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