Make audio only play once

Hi, in my game I’m trying to play an audio file when a random generated number is equal to a pre-given number. It works, the only problem is the audio file playes a lot of times. I have tried using a boolean but it doesn’t work. Here’s the code:


#pragma strict

var sound1 : AudioClip;
var sound2 : AudioClip;
var sound3 : AudioClip;
var sound4 : AudioClip;
var sound5 : AudioClip;
var lastsound : String;
var randomnummer : int;
var randomsound : int;
var playsound : boolean = false;
var time : float = 1.0;
var GuiVisible : boolean;
var GuiVisibleInt : int;

function Start () {

lastsound = "no sound played";						// This is just for making the gui look nicer.

}

function Update () 
{

time += Time.deltaTime;

	if (time >= 3)									// If there are more than 3 seconds
	{
	randomnummer = Random.Range(1,10);				// Generate random number between 1 and 10
	randomsound = Random.Range(1,5);				// Generate random number between 1 and 5
	time = 0;										// Set the time back to 0
	}

	if (randomnummer == 5)							// If the first random number is equal to 5
	{
		playsound = true;							// Set boolean to play a sound true
	}	
	
	if (playsound == true && randomsound == 1)		// If playsound is true and the second random number is equal to 1
	{
		audio.PlayOneShot(sound1);					// Play the first audio file
		playsound = false;							// Set playsound back to false
		lastsound = "_sound1";						// This if for GUI
	}
	
	if (playsound == true && randomsound == 2)		// If playsound is true and the second random number is equal to 2
	{
		audio.PlayOneShot(sound2);					// Play the second audio file
		playsound = false;							// Set playsound back to false
		lastsound = "_sound2";
	}
	
	if (playsound == true && randomsound == 3)		// If playsound is true and the second random number is equal to 3
	{
		audio.PlayOneShot(sound3);					// Play the third audio file
		playsound = false;							// Set playsound back to false
		lastsound = "_sound3";
	}
	
	if (playsound == true && randomsound == 4)		// If playsound is true and the second random number is equal to 4
	{
		audio.PlayOneShot(sound4);					// Play the fourth audio file
		playsound = false;							// Set playsound back to false
		lastsound = "_sound4";
	}
	
	if (playsound == true && randomsound == 5)		// If playsound is true and the second random number is equal to 5
	{	
		audio.PlayOneShot(sound5);					// Play the fifth audio file
		playsound = false;							// Set playsound back to false
		lastsound = "_sound5";
	}
	
	if(Input.GetKeyDown("space"))					// This is for toggeling the GUI visible and invisible. I know there are
	{												// better ways of doing this (with getkeyup for example) but I'm to lazy
		GuiVisibleInt ++;							// to change it. 
	}

	if(GuiVisibleInt == 2)
	{
		GuiVisibleInt = 0;	
	}
	
	if(GuiVisibleInt == 1)
	{
	GuiVisible = true;
	}
	
	if(GuiVisibleInt == 0)
	{
	GuiVisible = false;
	}	
	
}

function OnGUI ()									// more gui stuff
{

if(GuiVisible == true)
	{
		GUI.Label(Rect(10,65,Screen.width,Screen.height),"Random Number : "+randomnummer);
		GUI.Label(Rect(10,85,Screen.width,Screen.height),"Random Nummer Timer : "+time.ToString("f2"));
		GUI.Label(Rect(10,105,Screen.width,Screen.height),"Last sound played : "+lastsound);
	}
}

I know it isn’t the best piece of code but I’m only one week into learning to code :wink:

Since the code is running in Update(), it is running every frame. You could wrap it all around an if (!audio.isPlaying) check.

Found it! If the random number is equal to 5 it puts the 5 directly back to 0 after the audio is played.

function Update () 
{

time += Time.deltaTime;

	if (time >= 3)									// If there are more than 3 seconds
	{
	randomnummer = Random.Range(1,10);				// Generate random number between 1 and 10
	randomsound = Random.Range(1,5);				// Generate random number between 1 and 5
	time = 0;										// Set the time back to 0
	}

	if (randomnummer == 5)							// If the first random number is equal to 5
	{
		playsound = true;							// Set boolean to play a sound true
		randomnummer = 0;							// Makes sure the audio is only played once
	}