Invoke acting strange
I am using an Invoke command to end my game after displaying an exit scene for a few seconds. When I open this scene I want it to display for a few seconds and then I want the game to stop. In the inspector I have "quitTime = 2" using UnityEngine; using System.Collections;
public class QuitGameAfterTime : MonoBehaviour {
public float quitTime;
void Awake ()
{
print ("QuitGameAfterTime: Awake, quitTime=" + quitTime);
Invoke("QuitGame", quitTime);
}
void QuitGame()
{
print ("QuitGameAfterTime: QuitGame");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#elif (UNITY_IPHONE)
Application.Quit();
#elif (UNITY_ANDROID)
Application.Quit();
#else
Application.Quit();
#endif
}
}
When I run my game and go directly to the exit scene everything works fine and the game ends after 2 seconds. Even if I go to a credits scene or goto a settings scene and then go to the exit scene everything works fine (game ends after 2 seconds). However if I play my game, leave the game scene and then go to the exit scene it does not work. The Awake function runs but the Invoke never does. This is a very consistent problem. I have no idea what could be part of my game to cause this?!?! Does anyone have any ideas?
I know this is a vague question, however does anyone have an idea of what could cause Invoke to NOT execute?
Answer by TGKG · Dec 23, 2015 at 05:46 PM
Okay, Problem Solved (I feel like a bit of a fool) !! The way I exit my game is to press the pause button and within the pause menu press the return button to get me to the main menu (I can tell some of you already know where this is going) :)
As it turns out, when I pause the game and press return I never reset the Time.time = 1.0. THUS, Invoke did not work on exit. So simple yet so obvious now!!
Before you ask, yes I had a Time.time = 1.0 when starting my game so everything else worked as it should.