- Home /
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 : http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.html ; 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.
I don't know about Destroy and Load issues, but you should change the UV of the cubes falling down in the menu so the sides are transparent, and use "Clamp" ins$$anonymous$$d of "Repeat" in the texture parameters, to avoid lines on the edges of your bubbles.
thanks, I shall check it out , as it was bugging me too :)
Answer by rabbitfang · Jan 27, 2012 at 01:36 AM
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
)
thankyou for the informative reply. you explained the point of class and object, which I shall check out more. I just started unity, with a little experience in flash (click homepage button on my game).
But i just had an afterthought after reading back on the other link; should I add :- else { instance = this; } before the destroy (i also see transform.gameObject is also this.gameObject).
as the static var and static function wasn't $$anonymous$$e, I had a little trouble understanding it, and missed the this statement.
i agree my na$$anonymous$$g is confusing on anything i put in Find. It started when I was learning what var belonged to what name. All my hierarchy is prefabDooDaa , scriptDooDaa and matDooDaa. I shall make the var name DooDaa_Script = scriptDooDaa in future :) many thanks
I have just applied your advice, and in the program i checked the hierarchy and the music player was NOT destroyed when going back to the main menu. I checked and the scripts are identical to the scripts on this page.
the problem is still there after testing a few times , so now I am very confused . and i don't want to cheat and do something bad like have a scene Start that is a duplicate of $$anonymous$$ain $$anonymous$$enu, create the $$anonymous$$usicPlayer in Start, then have Win/Lose go to $$anonymous$$ain $$anonymous$$enu where no $$anonymous$$usic Player is created. I didn't know what to do, so I have revised my original question (self answer removed, sry).
just tested a few times. the reason i thought it was working at first is on the first round from losing to starting again, the music player is not heard. but play again and lose again, and the error can be heard on 2nd return to the main menu.
@rabbitfang , Sorry it's taken so long for me to get back to this. When I first made this game and posted this question, I didn't have an understanding of classes or types(static, public, private). Now looking back on this project to fix the problem, I get it.
Thankyou for your answer, here's some long-overdue well earned karma =]