I have a skydiving game where I want the game to reset to the start menu so long after the player touches down. I have this code that I think should work, but for some reason isn’t.
void OnCollisionEnter (Collision collision){
if (collision.gameObject.tag == "terrain")
Invoke ("restart", 2);
}
void restart(){
SceneManager.LoadScene ("Start Menu");
}
I double checked that all of the tags are right, so it isn’t that, and no errors are coming up when I try to debug it. Any ideas as to what could be going wrong?
Have you added the “Start Menu” scene to the “Scenes in build” list in the Build menu?
It is done by opening the scene “Start Menu” and then open the “Build settings” Ctrl+Shift+B and then click the “Add Open Scenes” Button.
To Figure out if the Method is running like Filhanteraren mentions add Debug.log(“Whatever”); to the restart method and/or OnCollisionEnter method.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "terrain")
Invoke("restart", 2);
Debug.Log("OnCollisionEnter got called");
}
void restart()
{
SceneManager.LoadScene("Start Menu");
Debug.Log("restart got called");
}
As a sidenote collision.gameObject.CompareTag(“terrain”) is better to use than collision.gameObject.tag == “terrain” in regards to GC allocation.