How to Stop the background song and play the Death Song when Player is Dead?

Hi, I am making a Flappy Bird Game in Unity 4.3.4 I want to know How to Stop the background song and play the Death Song when Player is Dead in C Sharp Script?Please tell me in details because I want to learn as well not just copy.I am a beginner so I don’t know anything about scripting.

Note=It should be for Unity 4.3.4 not for any other version.

using UnityEngine;
using System.Collections;

public class BirdMovement : MonoBehaviour {

    Vector3 velocity = Vector3.zero;
    public float flapSpeed = 100f;
    float forwardSpeed = 1.05f;
	public AudioClip newSong;

    bool didFlap = false;

    Animator animator;

    public bool dead = false;
    float deathCooldown;

    public bool godMode = false;

	// Use this for initialization
	void Start () {
        animator = GetComponentInChildren<Animator>();

        if(animator == null) {
            Debug.LogError("Did not find animator!");
        }

	}
	
	// Everthing should be here
    void Update () {
        if(dead) {

			deathCooldown -= Time.deltaTime;
        
        if(deathCooldown <= 0) {
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
                    Application.LoadLevel(Application.loadedLevel);
            }
        }
        }
        else {
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
            didFlap = true;
        }   
    }
        if (Input.GetKey(KeyCode.Escape)) {
            Application.Quit();
         }
  }

So first thing first, we will need to make a reference to the audio source. Note this is not an audio clip. Along with this, the newSong that you have should also be an audio source and not clip. I believe you have the sources set up somewhere from dragging the song to the hierarchy, so they probably have their own gameobjects, but that doesn’t really matter. For this we just need the audio sources.

public AudioSource newSong;
public AudioSource deathSong;

Before we get onto scripting, a key part of this is that we don’t want the deathSong to start playing immediately, which would happen if we just left it as is. To stop this we need to inspect the gameobject in the scene and uncheck the “Play On Awake” checkbox. This stops the audio source from playing when the level loads, and requires scripting in order to start the song.

Next we get into the scripting portion. We will be using the .Play();(1) and .Stop();(2) to control the songs. Now if we just plopped those into the if (dead) { portion, it would result in an “Endless First Note” situation, where every frame it would just restart the death song (I believe that is what would happen). In order to keep that from happening, we need to use the if (.isPlaying)to keep it from re-playing the first note. The following script will go inside the if (dead) { section of your script.

bool PlayedDeathSong = false;

Add above script into the variable calling area to prevent looping of death song

if (newSong.isPlaying) {
     newSong.Stop();
}
if (!deathSong.isPlaying && PlayedDeathSong == false) {
     deathSong.Play();
     PlayedDeathSong = true;
}

The first if statement means that if the original background music is still playing while the player is dead, it will stop that music. The second if statement means that if the death music is not playing while the player is dead, it will start that audio source.

If you need any further explanation on this, or it doesn’t work in Unity 4.3.4 for some reason, I am willing to help you through!

It would be easier if you put both your audioclips in an audiosource component (1 component per object, so make a child and add it to that)(Unity - Manual: Audio Source) then make 2 AudioSource variables (public AudioSource Music; and DeathMusic) then when you die, you just stop the Music from playing using Music.Stop(); and play the death music with DeathMusic.Play

Or you can try this:

public AudioClip dead; // You can drag the Audioclip in editor
    bool gameover;
    
    void Update()
    {
         if (gameover)
            SoundOnGameOver();
    }
    
    void SoundOnGameOver()
    {
        AudioSource audio = Getcomponent<AudioSource>();
        audio.Stop();
        audio.clip = dead;
        audio.Play();
    }