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
1
Question by coughin05 · Mar 30, 2015 at 09:28 AM · javascriptfunctionjavayield waitforseconds

yield WaitForSeconds(); issue--very specific, apparently

I've been looking through other answers for a while now, but I still haven't been able to find a solution to my problem. Let me show you the code first. It's very simple:

 function GameOver ()
 {
     print("GameOver has started");
     yield WaitForSeconds(3);
     print ("Yield waitforseconds in GameOver has ended");
     Application.LoadLevel("screenLose");
 }

As it stands, nothing happens after yield WaitForSeconds(); in this code. The console prints "GameOver has started" just fine, but I never see the second print. If I remove the yield WaitForSeconds, everything works just fine.

This function is being called from another script, but like I said, it still works fine up until yield WaitForSeconds();.

Any ideas what's going wrong here?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by _joe_ · Mar 30, 2015 at 10:35 AM

You need to start the Coroutine and not simply call it.

So instead of GameOver(), you should write : StartCoroutine(GameOver()); or StartCoroutine("GameOver");

Comment
Add comment · Show 11 · 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 coughin05 · Apr 01, 2015 at 01:49 PM 0
Share

Tried this. It didn't change anything. :/ Thanks anyway.

avatar image _joe_ · Apr 01, 2015 at 01:53 PM 0
Share

If my suggestion didn't work, the problem should be somewhere else. Post the rest of the code so I check it

avatar image karl_ · Apr 01, 2015 at 01:56 PM 0
Share

If you're using JS the compiler will implicitly call the StartCoroutine for you (docs)[http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html].

avatar image coughin05 · Apr 01, 2015 at 02:01 PM 0
Share

Here, I'll give you the function that calls function GameOver(), because there's a lot of other code; most of it irrelevant:

 function PlayerReset ()
 {
     if (playerVulnerable == 0)
     {
         if (lives > 1)
         {
             PlayerInvulnerable ();
             Instantiate(explosion, transform.position, explosion.rotation);
             transform.position = startPosition;
             scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).LifeLoss();            //run the "LifeLoss" function in scriptScene$$anonymous$$anager (subtracts a life from the scene$$anonymous$$anager)
             lives -= 1;
         }
         else
         {
             Instantiate(explosion, transform.position, explosion.rotation);
             scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).GameOver();            //run the "GameOver" function in scriptScene$$anonymous$$anager
             Destroy(gameObject);
         }
     }
 }


Sorry -- the formatting got a little messed up. Thanks for the help.

avatar image _joe_ · Apr 01, 2015 at 02:06 PM 1
Share

Basically what i said in my Answer, you are directly calling the coroutine, which doesn't start it, you need to Start it using StartCoroutine.

Here you go :

Replace line 16: scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).StartCoroutine("GameOver");

Show more comments
avatar image
0

Answer by Bunny83 · Mar 30, 2015 at 11:51 AM

Is it possible that you have set Time.timescale to 0?

Does it work when you replace the yield with

 yield null;

This of course won't wait 3 seconds, it's just a test to see if it works.

edit

Well, the way you call the coroutine is the actual problem. When using UnityScript (Unity's "Javascript") you don't need to call StartCoroutine because the compiler does this automatically for you. The problem is that you use StartCoroutine of this script and not on the one where the coroutine is actually located. So you actually do:

 this.StartCoroutine(sceneManager.GetComponent(scriptSceneManager).GameOver());

This will run the coroutine on the current object (the one this script is attached to). This usually isn't a problem, but since you immediately call

 Destroy(gameObject);

after that line, this object is destroyed and all coroutines that run on that object.

In your case you have to use:

 var sm = sceneManager.GetComponent(scriptSceneManager);
 sm.StartCoroutine(sm.GameOver());

However it's actually not a good idea to start a coroutine that way. You should make the actual coroutine private and invoke it from a "normal" method. So inside your "scriptSceneManager" class it should look like this:

 function GameOver()
 {
     GameOverCo();
 }
 
 private function GameOverCo ()
 {
     print("GameOver has started");
     yield WaitForSeconds(3);
     print ("Yield waitforseconds in GameOver has ended");
     Application.LoadLevel("screenLose");
 }

It looks totally silly and looks like it's pointless to do that extra step, but the compiler actually creates this code:

 function GameOver()
 {
     this.StartCoroutine(GameOverCo());
 }

Now when you call the GameOver method from another object, the coroutine will run here on the "scriptSceneManager" object since we now use StartCoroutine of this object and not the one of the calling object.

Comment
Add comment · Show 2 · 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 coughin05 · Apr 01, 2015 at 01:51 PM 0
Share

I tried this and had the same problem. So maybe it is a timescale issue, but operations I've used so far that use Time.deltaTime have been working perfectly. Any other ideas?

avatar image Bunny83 · Apr 01, 2015 at 06:16 PM 0
Share

Yet another case where that so called "simpler syntax" of UnityScript causes unexpected results. UnityScript makes certain phrases of code shorter, but not simpler.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with enum and stuff (right, left click) in JavaScript 0 Answers

Calling Functions Within A Script? 1 Answer

4.6 - Using new GUI with JavaScript functions 1 Answer

Problem accessing variable in javascript 1 Answer

how to delay a object from moving 1 Answer


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