Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by fred_gds · Nov 08, 2014 at 11:21 PM · iosstartinvoke

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrSoad · Nov 08, 2014 at 11:27 PM 0
Share

Have you tried making the Invoke trigger time say 30 rather than 2 just to see what it does?

1 Reply

· Add your reply
  • Sort: 
avatar image
8

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fred_gds · Nov 08, 2014 at 11:47 PM 0
Share

just tried it, but it didn't affect the results :/

could somebody please make this a comment. thanks.

avatar image MrSoad fred_gds · Nov 08, 2014 at 11:48 PM 0
Share

If you click on the more tab you should have the option to change it to a comment.

avatar image moh05 fred_gds · Nov 23, 2016 at 12:12 PM 0
Share

Same here, did you come up with a solution?

avatar image fred_gds moh05 · Nov 29, 2016 at 10:40 PM 0
Share

If I recall correctly I did not find a solution and since then haven't looked back into it.

Show more comments
avatar image _Shockwave · Jul 30, 2016 at 08:42 PM 0
Share

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

avatar image Hoorza · Nov 11, 2017 at 10:36 AM 0
Share

Such a simple little thing. Time.TimeScale =1 fixed everything. Thank you so much.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges