- Home /
GUI buttons not appearing on Destroy
So I created a final stage for my game that is only a black screen with music playing in the background, and after the music finishes a GUI button should appear that allows the player to return to the beginning.
I am not very knowledgeable in c# so I set this up where there is an object set to destroy in 10 seconds (the music) and then execute the script to bring up the script, so that it works similar to a player dying and given a "return to menu" button. The scripts I've used for previous stages have worked as intended, however this single stage will not execute the script, or they are simply hidden somehow.
The two method's I've tried using to execute it are
void OnDestroy()
{
transform.parent.gameObject.AddComponent<GameOverScript4>();
}
with the object being hit by a projectile and dying and
void FixedUpdate ()
{
Destroy(gameObject, 10);
}
void OnDestroy()
{
transform.gameObject.AddComponent<GameOverScript4>();
}
Where the object just times out.
The former I used for actual player deaths and the latter I reworked from a script that executed the next stage after a set time. These lines executed exactly as intended in all other stages, but don't seem to work on this single stage.
I have no idea what other information would be helpful so please ask for details.
Answer by Midnaut · May 26, 2014 at 06:51 AM
So what you've done is destroy the GameObject you're trying to add the game over script to.
Once you destroy a GameObject it is removed from the scene, therefore no scripts will run. So if you want the script to run, add it to another object in the scene like the main camera or you could make a prefab that has the script added then instatiate.
https://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Also key to remember that OnDestroy is not called until after the time delay in Destroy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145911im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IGUI IOS error 1 Answer
Health Bar Percentages Problem 2 Answers
ExecuteEvents.Execute pass object to from ui 1 Answer
Distribute terrain in zones 3 Answers
How to force refresh mouse position? 2 Answers