Invoke not working from Start() ?

Hey,

I’m currently facing a strange issue. As I have a scene with a script in which I call

  1. Start()
    {
    Invoke(“myFunction”, 2);
    Debug.Log(“Start called”);
    }
  2. function myFunction ()
    {
    Debug.Log(“myFunction called”);
    }

So this works perfectly as long as I load that scene in the editor as the first scene. Now if I call that scene over Application.loadLevel() the Invoke will not be called. But if don’t call myFunction as an Invoke() but simply as myFunction() it will get called. I don’t get any Errors on that and as I get the Debug “Start called” I am assuming that the invoke somehow gets executed?!

I know that there are other coding possibilities to achieve the same effect as with the Invoke function, but I would like to know why an Invoke inside of Start() won’t work when the scene is loaded via Application.loadLevel() but does when I start it. That doesn’t make any sense to me and I couldn’t find any information on that.

I’m using Unity 4.5.5f1 for Mac developing for iOS if that might matter.

[Edit: Scroll down for the solution]

Hi there.
I’m experiencing the same issue.
Further more, if I call Invoke (method, 0) it will work. If I replace the “0” with any other number, then the Invoke only works as described by the OP (only if the level is loaded first, and not when the level is loaded using Application.LoadLevel.

I have also tried to put the level inside void OnLevelWasLoaded, and then I’ve used Application.SendMessage(“OnLevelWasLoaded”, Application.loadedLevel) in Start. Still doesn’t work as expected.

Code:
void Start()
{
gameObject.SendMessage (“OnLevelWasLoaded”, Application.loadedLevel); // This
}

void onLevelWasLoaded( int level )
{
    splashPanel.SetActive (true);
    Invoke ("WaitCloseSplash", 0.5F);
}

void WaitCloseSplash ()
{
    splashPanel.SetActive (false);
}

OP, can you confirm that if you call Invoke with 0 it works?
Question: Did you find any workarounds? I’ve been fiddling with this for a couple of days now.

Code that did not work:

void Start()
{
    Invoke("WaitCloseSplash", 1); // works if 1 is replaced with 0
}

void WaitCloseSplash()
{
    splashPanel.SetActive (false);
}

void Start()
{
    StartCoroutine("WaitCloseSplash");
}

IEnumerator WaitCloseSplash()
{
    yield return new WaitForSeconds(2); // works if 1 is replaced with 0
    splashPanel.SetActive (false);
}

The Real Culprit:

Time.timeScale = 0;

Somewhere in another file the game was paused before returning to the scene that was running Invoke or StartCoroutine + yield return new waitforseconds

The Solution:

time.timeScale = 1; // un-pause the game before switching levels
Application.LoadLevel(0); // or any other level

Or:

Time.timeScale = 1; // Make sure that the game is not paused before calling StartCoroutine and using yield return new WaitForSeconds
StartCoroutine( "WaitCloseSplash" );

OR:

Time.timeScale = 1; // Make sure that the game is un-paused before waiting.
yield return new WaitForSeconds(1);

Or:

time.timeScale = 1; // Make sure that the game is not paused before using Invoke
Invoke("WaitCloseSplash", 2); // My preferred way of delaying execution.

The Short Answer: Make sure that the game is not paused before using any execution delay techniques!

P.S. I know that this thread is somewhat old, but I’m posting here because there is no answer provided yet and maybe someone will stumble across this when searching for a solution, just like I did.