I have a code that functions as my health bar. When the player dies, there’s an audio clip that I want too play only one time. Here’s the script (the audio play is on line 217)…
var health1 : UnityEngine.UI.Image;
var health2 : UnityEngine.UI.Image;
var health3 : UnityEngine.UI.Image;
var health4 : UnityEngine.UI.Image;
var health5 : UnityEngine.UI.Image;
var health6 : UnityEngine.UI.Image;
var health7 : UnityEngine.UI.Image;
var health8 : UnityEngine.UI.Image;
var health9 : UnityEngine.UI.Image;
var health10 : UnityEngine.UI.Image;
var fadeTime : float = 0;
var health1Trigger : GameObject;
var health2Trigger : GameObject;
var health3Trigger : GameObject;
var health4Trigger : GameObject;
var health5Trigger : GameObject;
var health6Trigger : GameObject;
var health7Trigger : GameObject;
var health8Trigger : GameObject;
var health9Trigger : GameObject;
var health10Trigger : GameObject;
var screenFuzz : UnityEngine.UI.Image;
var screenGlitch : UnityEngine.UI.Image;
var deathScreen1 : UnityEngine.UI.Image;
var deathScreen2 : UnityEngine.UI.Image;
var deathText1 : UnityEngine.UI.Text;
var deathText2 : UnityEngine.UI.Text;
var loadingBarBorder : UnityEngine.UI.Image;
var loadingBarEmpty : UnityEngine.UI.Image;
var loadingBar : UnityEngine.UI.Image;
var loadingText : UnityEngine.UI.Text;
var deathTimer : Light;
var glitchSound : AudioClip;
var timerSpeed : float = 0;
var loadTime : float = 0;
var enter : boolean;
function Start () {
health1.color = Color.green;
health2.color = Color.green;
health3.color = Color.green;
health4.color = Color.green;
health5.color = Color.green;
health6.color = Color.green;
health7.color = Color.green;
health8.color = Color.green;
health9.color = Color.green;
health10.color = Color.green;
health1Trigger.SetActive (false);
health2Trigger.SetActive (false);
health3Trigger.SetActive (false);
health4Trigger.SetActive (false);
health5Trigger.SetActive (false);
health6Trigger.SetActive (false);
health7Trigger.SetActive (false);
health8Trigger.SetActive (false);
health9Trigger.SetActive (false);
health10Trigger.SetActive (false);
}
function Update () {
if (enter)
{
health1Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
{
health2Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
{
health3Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
{
health4Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
{
health5Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
if(health5.color == Color.clear)
{
health6Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
if(health5.color == Color.clear)
if(health6.color == Color.clear)
{
health7Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
if(health5.color == Color.clear)
if(health6.color == Color.clear)
if(health7.color == Color.clear)
{
health8Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
if(health5.color == Color.clear)
if(health6.color == Color.clear)
if(health7.color == Color.clear)
if(health8.color == Color.clear)
{
health9Trigger.SetActive (true);
}
if (enter)
if(health1.color == Color.clear)
if(health2.color == Color.clear)
if(health3.color == Color.clear)
if(health4.color == Color.clear)
if(health5.color == Color.clear)
if(health6.color == Color.clear)
if(health7.color == Color.clear)
if(health8.color == Color.clear)
if(health9.color == Color.clear)
{
health10Trigger.SetActive (true);
}
if (health1Trigger.activeSelf)
{
health1.color = Color.Lerp (health1.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health2Trigger.activeSelf)
{
health2.color = Color.Lerp (health2.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health3Trigger.activeSelf)
{
health3.color = Color.Lerp (health3.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health4Trigger.activeSelf)
{
health4.color = Color.Lerp (health4.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health5Trigger.activeSelf)
{
health5.color = Color.Lerp (health5.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health6Trigger.activeSelf)
{
health6.color = Color.Lerp (health6.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health7Trigger.activeSelf)
{
health7.color = Color.Lerp (health7.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health8Trigger.activeSelf)
{
health8.color = Color.Lerp (health8.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health9Trigger.activeSelf)
{
health9.color = Color.Lerp (health9.color, Color.clear, fadeTime * Time.deltaTime);
}
if (health10Trigger.activeSelf)
{
health10.color = Color.Lerp (health10.color, Color.clear, fadeTime * Time.deltaTime);
screenFuzz.enabled = true;
screenGlitch.enabled = true;
Time.timeScale = 0.01;
deathTimer.GetComponent.<Light>().intensity -= 0.1 * Time.deltaTime / timerSpeed;
GetComponent.<AudioSource>().PlayOneShot(glitchSound);
}
if (deathTimer.GetComponent.<Light>().intensity == 0)
{
deathText1.enabled = true;
deathScreen1.enabled = true;
loadingBarBorder.enabled = true;
loadingBarEmpty.enabled = true;
loadingBar.enabled = true;
loadingText.enabled = true;
loadingBar.GetComponent.<RectTransform>().anchoredPosition.x += 0.1 * Time.deltaTime / loadTime;
}
if(loadingBar.GetComponent.<RectTransform>().anchoredPosition.x >= -3)
{
deathText2.enabled = true;
deathScreen2.enabled = true;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Damage") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Damage") {
enter = false;
}
}
But, my issue is, whenever that audio file plays, it plays over and over again, and the audio overlaps. How could I make this only play one time and one time only.