- Home /
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);
}
Well I found the problem, its in the awake function so its playing it 100 times over. Now to fix :/
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
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.
Good suggestion - its played on a button down. Ill update the post with the code.
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
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)
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
Very helpful thanks much I will look at those as well!
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.
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.
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);
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 =]
Your answer
![](https://koobas.hobune.stream/wayback/20220613085210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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