- Home /
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
Start() {
Invoke("myFunction", 2); Debug.Log("Start called"); }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.
Have you tried making the Invoke trigger time say 30 rather than 2 just to see what it does?
Answer by darius.mihai · Mar 07, 2015 at 09:56 PM
[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.
just tried it, but it didn't affect the results :/
could somebody please make this a comment. thanks.
If you click on the more tab you should have the option to change it to a comment.
If I recall correctly I did not find a solution and since then haven't looked back into it.
Thank you so much, you saved me from a headache after wasting 30 $$anonymous$$s trying to figure out why Invoke wasn't working! Cheers :D
Such a simple little thing. Time.TimeScale =1 fixed everything. Thank you so much.
Your answer
Follow this Question
Related Questions
Player Movement by Touch in only four Direction. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
hide object start script 4 Answers
On iOS Start() getting called when app resumes 0 Answers
How do I load the first level? 2 Answers