- Home /
Why doesn't Destroy work?
I am trying to make it so that in the pause menu you can change your character. I want to destroy the original player. However, this isn't working. Any ideas? Boom() destroys the object.
#pragma strict
var pause = false;
var RIP;
var prefab : Transform;
var CS : GameObject;
var SS : GameObject;
function Start () {
transform.position = CS.transform.position;
Instantiate (SS, transform.position, Quaternion.identity);
}
function Update () {
if (Input.GetKeyUp ("escape")) {
pause = true;
Time.timeScale = 0;
Debug.Log("babaam");
}
}
function OnGUI() {
if (pause) {
if (GUI.Button(Rect(Screen.width / 2,Screen.height / 2 - 40,100,30),"Play")) {
Time.timeScale = 1;
pause = false;
Debug.Log("Clicked the button with text");
}
if (GUI.Button(Rect(Screen.width / 2,Screen.height / 2,100,30),"Switch")) {
transform.position = CS.transform.position;
Boom();
Instantiate (prefab, transform.position, Quaternion.identity);
Time.timeScale = 1;
pause = false;
Debug.Log("Clicked the button with text");
}
}
}
function Boom() {
RIP = GameObject.Find("player");
Destroy(RIP);
}
It seems that everything is fine on your code.
Did you check if your player object is activated? and if there isn't any spelling error with the name of the object?
You're using pragma strict, try defining RIP's type like you did with your other variables. Also try using a Debug.Log statement in your Boom function, to make sure the function is being fired. Check the case in the spelling of "player" as well.
var RIP : GameObject;
Haha. my fault. It was instantiating the player as player(Clone).
Then you should accept @RyanZimmerman87's answer, since he mentions this. I'll go ahead and to so, glad you got this resolved.
Answer by RyanZimmerman87 · Sep 07, 2013 at 04:03 AM
So is this script attached to an empty object for just the pause menu and some logic? Not attached to the players you are destroying right?
Yeah the solution is not popping out in any obvious way to me. I would probably call the boom() function at the end of your button press logic, I like to keep things ordered sequentially especially when dealing with functions calls which on occasion the position of them has messed things up for me in the past.
Also you might need to do
RIP = GameObject.Find("player(Clone)");
Depending on how you created the original player, but you can see what the name is in Hierarchy.
Not sure if either of those are your problem but that'd be the first think I would check. I also only use C# so maybe I'm missing something else.
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Game Over GUI Question 2 Answers
Unity freezes when changing GUI.Color.a 1 Answer
How can i show the texture in the array? 2 Answers
Detect Text in GUI; Print 1 Answer