- Home /
Audio doesn't play when collecting a coin
Hello!
I want the game to play a sound when the player collects a coin, but the sound doesn't play. What am I doing wrong?
public GameObject Coin;
public AudioClip soundFile;
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
CoinController.coins++;
CoinController.coinsCollectedThisLevel++;
PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
audio.clip = soundFile;
audio.Play();
Destroy (Coin.gameObject);
}
}
Could be a number of things:
Is the AudioSource you're attempting to play from attached to the GameObject you're destroying? If so, you won't hear the clip because you destroy it on the same frame. Try the static AudioSource.PlayOneShot and see if that works.
Is the sound set as a 3D sound and the scene's audio listener is too far away to hear it? You may want to go to the import settings and make the coin pickup sound 2D.
Check these two things and report back.
Changed the audio file to 2D sound and changed the script. Still doesn't work. :(
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
CoinController.coins++;
CoinController.coinsCollectedThisLevel++;
PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
audio.PlayOneShot(soundFile, 1);
Destroy (Coin.gameObject);
}
}
Answer by Chris333 · Jan 21, 2015 at 08:33 PM
Hi, i think Broxxar already found the Problem. You play the clip and destroying the object at the same time. The audio reference returns the AudioSource of the gameObject. http://docs.unity3d.com/ScriptReference/GameObject-audio.html
Try to call destroy with the delay of the length of your audio clip.
Destroy(Coin.gameObject,audio.clip.length);
This definitely worked, thanks to you and Broxxar. However if I use Destroy(Coin.gameObject,audio.clip.length); The collection of the coin looks kind of stupid. I'll have to think of another way.
Just use a central AudioSource in your scene which plays the sounds. Than you dont need to delay the destroy $$anonymous$$ethode.
What you might want to do is have the coin point to an external AudioSource attached to the character or camera. In the code where it destroys itself it tells the AudioSource to play before it destroys itself.
Then the AudioSource is not destroyed with the coin but the coin disappears instantly.
Try this:
public GameObject Coin;
public AudioClip soundFile;
public AudioSource $$anonymous$$C;
void Start () {
$$anonymous$$C = Camera.main.GetComponent<AudioSource>();
}
void OnTriggerEnter ()
{
if (Coin.gameObject.tag == "Coin") {
CoinController.coins++;
CoinController.coinsCollectedThisLevel++;
PlayerPrefs.SetInt ("Coins", CoinController.coins + 1);
$$anonymous$$C.PlayOneShot(soundFile);
Destroy (Coin.gameObject);
}
}
Answer by sniper43 · Jan 21, 2015 at 10:53 PM
Boxxar has found your problem, But here's another solution:
Replace
audio.Play();
with
AudioSource.PlayClipAtPoint(audio, Coin.transform.position);
This creates a seperate game object that is a sound source but has no body and self destructs after the audio clip is over.
This is better assuming the object has a collider and you don't wish for an animation to play.
When you have an animation it is better to use
Destroy(Coin.gameObject, YourAnimationObjectHere.clip.length)
I got the idea from http://unity3d.com/learn/tutorials/projects/stealth/the-key
You shold do the stealth tutorial if you haven't already.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[SOLVED] Issue with music and Don't Destroy on load 2 Answers
[c#]Does audio.Play() work only in Update and Start funcitons? 2 Answers
Text not setting to correct value 1 Answer