Won't go to next scene
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?
Add a debug in your restart method to see if it gets called
How would I do that? Sorry, haven't done any coding before and have gotten rather lost.
Answer by Pengocat · Dec 14, 2016 at 03:24 AM
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.
Yeah, and it will go from the menu to the game scene just fine- part of what was baffling me about why the other way wasn't working.
Thanks for explaining how to do the debugger, somehow neither the OnCollisionEnter or the restart is getting called. I ended up changing it to invoke the restart when they got below a certain height and that is working for now, although still curious as to why the other way won't work.
Thanks!