Change sound depending health

Hello Unity3d community

I am an absolute beginner and in need of your expertise.

How can I control the music based on my lifebar? I’ve searched a lot but did not find any working solution.

What I have achieved so far , is that the music starts properly. But it repeats itself over and over again (like an echo) . How can I start an audio in an update only once?

At the end I’ve would like to have something like this:

Player has 1 or 2 Lives = Play Music A

Player has 3 or 4 Lives = Play Music B

Player has 5 or 6 Lives = Play Music C

Here the code:

#pragma strict

var Life12 : AudioClip;

var Life34 : AudioClip;

var Life56 : AudioClip;

function Update () {

if (GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life >= 0 && GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life < 2 )

     Life12Sound();

if (GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life >= 3 && GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life < 4 )

     Life34Sound();

if (GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life >= 5 && GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life < 6 )

     Life56Sound();
}

function Life12Sound(){

    yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
    GetComponent.<AudioSource>().Stop();
    GetComponent.<AudioSource>().PlayOneShot(Life12);
} 

function Life34Sound(){

    yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
    GetComponent.<AudioSource>().Stop();
    GetComponent.<AudioSource>().PlayOneShot(Life34);
} 

function Life56Sound(){

    yield WaitForSeconds (GetComponent.<AudioSource>().clip.length);
    GetComponent.<AudioSource>().Stop();
    GetComponent.<AudioSource>().PlayOneShot(Life56);
}  

I appreciate your support and send friendly greetings from Switzerland

you dont need any coroutine … this just works fine for me:


edit: ohh btw … its in C#, i just saw you are using unityscript, unityscript solution can be found here: #pragma strict var AudioTest : AudioClip;var MyClips = new AudioClip[3]; - Pastebin.com.


edit2: some dont’s:

  • don’t call everyframe GetComponent<…>, save it in a variable in your start() method
  • don’t use GameObject.Find(“”) if you can avoid it
  • DRY: don’t repeat yourself. if you need much copy and paste in your code, you are probably doing something wrong
  • (don’t use unityscript :stuck_out_tongue: - C# is such a beautiful language)

  class AudioTest : MonoBehaviour {
    public AudioClip[] MyClips = new AudioClip[3]; // filled in the editor
    public float[] HealthSoundLimits = {2, 4, 6}; // may be changed in the editor

    public Lifebar Lifebar; // can be filled in the editor

    private int currentClip = -1;
    private AudioSource MySource;

    private void Start() {
        MySource = GetComponent<AudioSource>();
        if (Lifebar == null)
            Lifebar = FindObjectOfType<Lifebar>();
    }


    private void Update() {
        if (Lifebar.life <= 0) {
            MySource.Stop();
            return;
        }
        if(HealthSoundLimits.Length != MyClips.Length) return;
        for (var i = 0; i < HealthSoundLimits.Length; i++) {
            if (Lifebar.life >= HealthSoundLimits*) continue;*

MySource.clip = MyClips*;*
if (!MySource.isPlaying) MySource.Play();
break;
}
}
}
[68696-unbenannt.png|68696]_
_

I think it’s obvious - I’m bad in programming !

No matter what I try, it doesn’t works. I thank you from the bottom of my heart for your support and I am sure that we can solve it together … but I do not want to put any more of your precious time .

If you want to look at it anyway , then here’s the code :

#pragma strict
 
 var AudioTest : AudioClip;
 var MyClips = new AudioClip[3];
 var HealthSoundLimits = [2, 4, 6];
 
 var lifebar:float; // can be filled in the editor
     var currentClip = [-1];
     var MySource : AudioSource;
 
 
  function Start () {
     MySource = GetComponent.<AudioSource>();
             lifebar = GameObject.Find( "Lifebar" ).GetComponent( Lifebar ).life;
  }
 
 function Update() {
         if (lifebar >= 3) {
               MySource.Stop();
               return;
          }
 }
          if(HealthSoundLimits.Length != MyClips.Length) return;
          for (var i = 0; i < HealthSoundLimits.Length; i++) {
              if (lifebar >= HealthSoundLimits*) continue;*

MySource.clip = MyClips*;*
if (!MySource.isPlaying) MySource.Play();
break;
}
… I needed to change the things you mentioned - but the “FindObjectOfType” did not work, so I left it like it was before. Right now I don’t get any error-messages … that’s probably a good sign. But it stills not working … here the picture of de component:
[68709-bildschirmfoto-2016-04-23-um-182836.png|68709]_
When I start the game, the “lifebar” Section shows the right amount of lives … but does not update.
I know that I’ve a lot to learn! Appreciate your time and support!
_