In my game I am creating in unity I am trying to play an audio clip every time that a collision happens on one of my game objects. For some reason the audio will play the first time there is a collisiom, but from then on it no longer plaus the sound.
The game object that is collided with has a component for the AudioSource and I have the audio clip selected in that component.
Here is the code where I start the audio:
if (PlayerPrefs.GetInt ("Sound Playing", 1) == 1)
{
audio = GetComponent <AudioSource>();
audio.Play ();
}
I have also tried using the PlayOneShot () method but it does the same thing.
EDIT
Here is a small representation what of my game is doing here:
public class Ship : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider)
{
Laser laser = collider.gameObject.GetComponent<Laser> ();
if (laser)
{
if (!immune)
{
//update healthbar
healthbar.hit();
//reset health boost
forcefield.resetHealthBoost();
health -= laser.getDamage(); //remove health from ship
//create explosion
GameObject explosion = Instantiate(explode, gameObject.transform.position, Quaternion.identity) as GameObject;
explosion.GetComponent<ParticleSystem>().startColor = this.GetComponent<SpriteRenderer>().color;
if (SoundEffects.soundOn && PlayerPrefs.GetInt("Sound Playing", 1) == 1)
{
audio = GetComponent<AudioSource>();
audio.Play();
}
}
laser.hit();//destroy the laser that hit the ship
}
}
}
Here is to show that I have the AudioSource and the AudioClip setup in the inspector (the AudioSource is attached to the ship):

audio.Play()in particular) is being reached atleast two times by adding some debug statements. Maybe also try to callaudio.Stop()first before doingaudio.Play(), or have some additional logic for when the audio is currently being played. You should also observe the AudioSource component in the Unity Inspector to see if anything strange happens (component gets destroyed, clip is lost,..) - Maximilian GerhardtAudioSourcecomponent with a clip attached. The sound triggers indeed over and over again, each time I collide. So as long as something doesn't magically destroy theAudioListeneron your camera, or the clip is muted, I have no idea what's wrong. Can you provide your project / a minimal setup sothat it can be reproduced? - Maximilian Gerhardt