Calling audio in functions

#pragma strict

var audio1 : AudioClip;
var audio2 : AudioClip;
var audio3 : AudioClip;
var audio4 : AudioClip;

private var Health : HealthManager;

function Start()
{
    Health = GameObject.Find("CharacterControl").GetComponent(HealthManager);
}

function Update()
{
    if (Health.playerHealth >= 80)
    {
        GetComponent.<AudioSource>().Stop();
        GetComponent.<AudioSource>().clip = audio4;
        GetComponent.<AudioSource>().Play();
    }
    else if (Health.playerHealth >= 60)
    {
        GetComponent.<AudioSource>().Stop();
        GetComponent.<AudioSource>().clip = audio3;
        GetComponent.<AudioSource>().Play();
    }
    else if (Health.playerHealth >= 40)
    {
        GetComponent.<AudioSource>().Stop();
        GetComponent.<AudioSource>().clip = audio2;
        GetComponent.<AudioSource>().Play();
    }
    else if (Health.playerHealth >= 20)
    {
        GetComponent.<AudioSource>().Stop();
        GetComponent.<AudioSource>().clip = audio1;
        GetComponent.<AudioSource>().Play();
    }
}

I’m calling a few AudioClips to play when the player is damaged, with the variable being the current health after you take damage. How exactly would I stop one sound and start another, because the code I’m using bugs out. I’m not sure if it plays the same clips over each other, or something else, i just know it doesn’t work. Any feedback is appreciated, thanks guys

GetComponent().PlayOneShot(audio4);

Does PlayOneShot only play the clip once? If easily possible, I would like it to loop (not necessary though).

yeah you need to work on your coding skills, Update gets called every frame so don’t put a play audio clip there, unless you have a flag set (bool check).

So when the players health is less than 80 you calling playing audio every frame, and then when the players health is less than 60, you are now calling it to play 2 audio clips every frame, because the players health is Less than 80 and also Less than 60.

Basically if the players health is Greater than 80, it is going to be greater than 40 and 60, so you will have all the audio clips playing at the same time.

You need to set a new function and call it when a players health is at a certain amount.