Confusing Problem While Making Death Script For 2D Platformer

I have made this code here to play a little tune when my character dies, and I have come to a road block. before you look at my code, you must know that I assigned all the variables through the editor, and also that it is written in JavaScript.
Here it is:

#pragma strict

var CharacterPosition : Transform;
var DeathSound : AudioSource;
var DeathRespawnPoint : Transform;
var DeathPoint : int = -15;
var AlreadyStarted : boolean = false;

function FixedUpdate () {
if(CharacterPosition.position.y == DeathPoint && AlreadyStarted == false){
AlreadyStarted = true;
DeathSound.Play();
if(DeathSound.isPlaying == false){
CharacterPosition.position = DeathRespawnPoint.position;
AlreadyStarted = false;
}
}
}

One last thing you might need to know, is that I used Debug.Log() to try to figure out what wasn’t working in the code, and found that the code that wasn’t running was:

if(DeathSound.isPlaying == false){
CharacterPosition.position = DeathRespawnPoint.position;
AlreadyStarted = false;
}

It seemed as if the code could not tell that the AudioSource had ended, but I did yet another test, and the editor was showing that it did, though the script was not reading it. It has really confused me.

Let me know if there is any other information you need to know to solve my conundrum, and thanks in advance.

P.S.If anyone knows a better way to make a falling death script that would allow music to be played before respawning.

You check if AlreadyStarted is false in this if statement:

if(CharacterPosition.position.y == DeathPoint && AlreadyStarted == false)

Then immediately set it to true. So that if statement is never going to evaluate to true again, so it will never check if the audio is playing or not except the very first frame.

Your direct float comparison will also get you into trouble.

Make a big collider which is your “death area”. Make that collider a trigger. Add your current script to the gameobject with the collider and modify to somethng like:

function OnTriggerEnter(other : Collider)
{
   if(other.tag == "Player")
   {
      DeathSound.Play();
     while(DeathSound.isPlaying)
     {
         yield;
     }

    other.transform.position = DeathRespawnPoint.position;
    }
}