audio loop on update?

Hello,

im having trouble with playing and audio clip once on update. not sure if what i’m is the best approach so i’m wonder of the alternatives to my dilemma.

i’m trying to accomplish: with a boolean just spinning the object and playing a sound with it…
So if true it spins and plays a sound that loops. if false everything stops for now.

But when the boolean is true the audio clip just keeps playing every frame. i haven’t been able to find or figure out a solution yet.

heres the code i have so far…

    var spin = true;
var Speed = 4.0;
var Clip : AudioClip;

function Start(){
	audio.loop = true;
	audio.clip = Clip;

}

function Update(){
	
	if(spin == true){
		transform.Rotate(Vector3(0,-1,0) * Speed);
		audio.Play();
		
	}
	else{
		transform.rotation = Quaternion.Euler(-90, 0, 0);
		audio.Stop();
	}
}

If the AudioSource is set to loop, which you force in Start(), you shouldn’t have to do anything extra.

As an alternative, you could check audio.isPlaying and only call audio.Play() if it isn’t. Otherwise you’ll get multiple copies of the clip going at the same time.