Unity audio solution

So I’ve been looking around and can’t find a way for audio to work in my game.

I think I may be going about this in the wrong way so if someone could point me in the right direction that’d be fantastic.

I have the following script, which is set on a cube with an audiosource. I intended to have all the games sounds (excluding music) run on this script/object seeing as they’re 2D sounds. When lightning is active I want it to play the sound, which it does, one time. After that, no other sounds work, like the powerup sound or if another lightning object is active.

The soundStop function was just me trying to see if stopping audio after time would help but it didn’t. I just can’t seem to figure out audio in Unity. Everything else I’m okay with.

@script RequireComponent(AudioSource)
var powerUp : AudioClip;
var playerHit : AudioClip;
var enemyHit : AudioClip;
var lightningStrike : AudioClip;
var lightningStrike2 : AudioClip;
var enemyDie : AudioClip;

static var waiting : boolean = false;

function Start () {

DontDestroyOnLoad(this);

}

function Update () {

if (PlayerCollision.lightningStrike == true && waiting == false){

waiting = true;
soundStop();
//audio.clip = lightningStrike;
audio.PlayOneShot(lightningStrike);

}

if (PlayerCollision.pickupItem == true && waiting == false){

waiting = true;
soundStop();
//audio.clip = powerUp;
audio.PlayOneShot(powerUp);

}

}


function soundWait(){

yield WaitForSeconds(0.1);

waiting = false;

}

function soundStop () {

yield WaitForSeconds(1);

audio.Stop();

}

The main bug is that you’re never calling soundWait() and so waiting stays set to true and no other sounds are played.

audio.PlayOneShot() essentially creates a new game object with a new AudioSource on it with the specified clip. That’s why your audio.Stop() doesn’t work because you’re stopping the AudioSource on this object, but the sound effect is on the new object created by PlayOneShot.

PS - Please remember to mark answers as accepted when you are satisfied.