game object is not being destroyed

Hello all. This is my first question, though I have many ! So … I have an empty gameObject prefabMusicPlayer, that carries through the scenes playing music. I wish to destroy it on the win/lose screen as the main menu has different audio. During testing this code in the GUI function worked (from the sceneScreenWin/Lose script, not prefabMusicPlayer script):

var, and Find in start function on scriptScreenWin :-

// prefabMusicPlayer script
var MusicPlayerScript : scriptMusicPlayer;

// load MusicPlayerScript
MusicPlayerScript = GameObject.Find("prefabMusicPlayer").GetComponent(scriptMusicPlayer);

from the GUI function on scriptScreenWin :-

// Back Button    
       GUI.color = Color.green;    
       if (GUI.Button (Rect (10, 170, 180, 30), "Back to Menu"))    
       {    
         // DESTROY prefabMusicPlayer 
         //Destroy (MusicPlayerScript.gameObject);  // problem, added function    
         // wait so object is destroyed
         destroyAndWait ();

         // Load sceneScreenMainMenu    
         //Application.LoadLevel ("sceneScreenMainMenu");    
       }

But when I uploaded and ran from the website, the gameObject prefabMusicPlayer was not being destroyed. I added a function :

function destroyAndWait ()    
{    
    // DESTROY prefabMusicPlayer    
    //Destroy (MusicPlayerScript.gameObject);

    // script from unity answers advice    
    Destroy(scriptMusicPlayer.GetInstance().gameObject);          

    // wait so object is destroyed    
    yield WaitForSeconds (0.2);

    // Load sceneScreenMainMenu    
    Application.LoadLevel ("sceneScreenMainMenu");    
}

… with no success. I found this example : Audio or Music to continue playing between scene changes - Unity Answers ; and added it to the scriptMusicPlayer (ADDITIONAL SCRIPT pt 1 & 2). Again, the gameObject still exists after returning to sceneScreenMainMenu, then sceneLevel1 is adding Another MusicPlayer :

the full scriptMusicPlayer :-

// Music Player Script  (object carried through levels)
// -------------------


//
var playerX : float = 35.0;
var playerY : float = 11.0;
var playerZ : float = 0.0;


// ----    

// ADDITIONAL CODE TO CHECK WHEN prefabMusicPlayer IS NOT DESTROYED - pt1

private static var instance : scriptMusicPlayer; // use the filename of the SCRIPT , not PREFAB

public static function GetInstance() : scriptMusicPlayer 
   {
      return instance;
   }

// ----

//
function Awake () 
{

    // ADDITIONAL CODE pt2

    if (instance != null && instance != this) 
         {
            Destroy(this.gameObject);
            return;
         } 

       // END ADDITIONAL CODE pt2     


    // carry prefabMusicPlayer through scenes
    DontDestroyOnLoad (transform.gameObject);
}

// ----

//
function Start () 
{
    // assign the instance (don't forget)
    instance = this;

    // play audio if music is enabled
    if (PlayerPrefs.GetInt("Music") == 1)
    {
        audio.Play();
    }
}

// ----

//
function FixedUpdate () 
{
    // move Music Player to audio listener 
    transform.position = Vector3(playerX, playerY, playerZ);

    //check if music is toggled to OFF
    if (PlayerPrefs.GetInt("Music") == 0)
    {
        // mute audio
        audio.mute = true;
    }
    else
    {
        // un-mute audio
        audio.mute = false;
    }
}

// ----

Any help would be greatly appreciated, as this would fix one of the last problems in my first game built with Unity ! (here’s the project so far : http://www.alucardj.net16.net/WebPlayer.html ). Thanks, Jay.

The reason that it isn’t being destroyed is that MusicPlayerScript is referencing the class, not a specific script object.

In your second script instance is not even being assigned.

In your Start() function of your scriptMusicPlayer, add instance = this;

Before you load the new scene, call Destroy(scriptMusicPlayer.GetInstance().gameObject);, which should remove the prefab.

Btw, your naming scheme is VERY confusing (got mixed up between MusicPlayerScript and scriptMusicPlayer)