Author Topic: yield hangs from OnClick()  (Read 2983 times)

wilmerlin

  • Guest
yield hangs from OnClick()
« on: January 22, 2013, 02:48:51 AM »
I was trying to setup a script on a UIButton to load a level. It worked fine until I decided that I needed to pause the load of the new level with a yield.  The CoRoutine gets called but it does not yield and instead just hangs.

It works fine if I remove the yield WaitForSeconds(delay) or set the delay to 0.  Can I not call a Coroutine from OnClick()?



 
  1.  
  2. var levelToLoad: int = 0;
  3. var delay: float = 1;
  4.  
  5. function OnClick() {
  6.         yield LoadLevelWithDelay();
  7. }
  8.  
  9. function LoadLevelWithDelay() {
  10.         yield WaitForSeconds(delay);
  11.         Application.LoadLevel(levelToLoad);    
  12. }
  13.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: yield hangs from OnClick()
« Reply #1 on: January 22, 2013, 10:16:34 AM »
NGUI has a script for this: LoadLevelOnClick.

wilmerlin

  • Guest
Re: yield hangs from OnClick()
« Reply #2 on: January 22, 2013, 10:58:13 AM »
It does, but can I insert a yield WaitForSeconds() in there before the Application.LoadLevel?   Or does it not work because of OnClick()?

My own function works until I attempt the yield.  I can work around this by marking a time to load and loading the level in Update() but I thought this might have been the cleaner way to do it.

wilmerlin

  • Guest
Re: yield hangs from OnClick()
« Reply #3 on: January 22, 2013, 11:17:16 AM »
Nevermind. I am a friggin' idiot.  Have the time scale set to 0 because the game is paused.  Doh.

gregba

  • Guest
Re: yield hangs from OnClick()
« Reply #4 on: March 12, 2013, 11:27:50 AM »
I have just spent 2 days banging my head against the coding wall with exactly the same problem, glad I found your post and suddenly realized I was doing exactly the same as you!  :o