- Home /
Can i Destroy GameObject on Load?
i want to destroy one gameObjet affter i restart my level, i press the button and then when it starts the level gameObject is Destroyed it's not in my game any more can i make this ?
Answer by NEXNET_Systems · Jun 08, 2015 at 05:57 PM
You'll need to add this code to where you want the object to be destroyed. If you press Enter, the current level will restart and destroy the given game object that you will specify. See here for more information on how you can destroy game objects. You can also change the key that you want to use that will trigger the object's destruction simply by changing the value in KeyCode.
http://docs.unity3d.com/ScriptReference/Object.Destroy.html
void Start()
{
//If the game restarts what should we do?
if (Input.GetKeyDown(KeyCode.Enter))
{
//Restart the current level
Application.LoadLevel(Application.LoadLevel);
// Destroy the game object
Destroy(gameObject);
}
}
iam having the main menu canvas in the same screen with my game scene ,when i lose i want to destroy my main menu canvas when i restart my game ,iam using GUI when button is pressed restart level then destroy game object but i can't seem to make it
Answer by crohr · Jun 08, 2015 at 05:50 PM
You should have your script destroy the object in the method OnLevelWasLoaded(int Level), this will be called whenever a level is loaded.