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
0
Question by Nikolay · Dec 06, 2010 at 04:45 AM · yieldswitchfunctions

Some yield mess

Hi there. I'll try to put this across in the clearest manner possible, because it's been really nerve-racking over the past couple of days trying to sort this one out. I have a switch statement in JS, something like this:

switch(x){ case 1: Debug.Log("line 1"); yield Function1(); Debug.Log("line 2"); break; case 2: Debug.Log("line 3"); yield Function2(); Debug.Log("line 4"); break; //a couple of cases more... }

function Function1(){ //do something and yield some other functions yield Function3(); Debug.Log("line 5"); //return }

function Function2(){ //do something and yield some other functions yield Function3(); Debug.Log("line 6"); //return }

function Function3(){ //do something yield some other functions }

Now, when x is 1, case 1 executes as expected, and the statements are printed as follows:

line 1

line 5

line 2

The bizarre thing occurs when x is 2, because only

line 3

and

line 6

are logged to the console, i.e. I never get to see "line 4", which is the command that should execute immediately after Function2() returns. At first I suspected it might be getting stuck in an infinite loop somewhere down the chain of function calls Function2() makes, so I inspected all of them (all the functions getting called during this procedure) and each and every one of them printed the Debug.Log messages, as they should. Besides, if execution were indeed left looping somewhere, "line 6" wouldn't be printed, would it?:/(all the functions in the chain are called thru yield, and "line 6" is the last line of code in Function2()) Now removing the yield from the Function2() call in case 2 would print "line 4" on the console, of course, but wouldn't produce the desired result from the game's perspective. What adds to the oddness of all this, is that removing the yield from the Function3() call in Function2() also results in "line 4" being printed, which is all the more bizarre, since "line 6" is logged anyway. Any advice would be most welcome!

N.

Comment
Add comment · Show 3
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 Justin Warner · Dec 06, 2010 at 05:43 AM 0
Share

It is a mess... not having all the code doesn't help much...

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

avatar image Nikolay · Dec 06, 2010 at 08:33 AM 0
Share

Thanks for commenting, mate. I think I've referred to that page about a thousand times so far hoping for an answer...no such luck I didn't paste the entire code because I didn't think it was necessary, rather I tried to be as concise as possible

avatar image Mike 3 · Dec 06, 2010 at 06:01 PM 0
Share

Try using the c# way of coding coroutines - it stops these silly random JS errors. The function type should be IEnumerator, and you should call the function with StartCoroutine(Function()); - it makes life a lot easier with huge chains of coroutines like this

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by skovacs1 · Dec 06, 2010 at 06:14 PM

Wow. What the heck are you trying to do with all of these Yields? I would wager your design is likely in need of some re-evaluation because this seems like a convoluted approach, but that's your call to make in the end.

I don't even know how you got it to run. I tried the code that you posted and it would not compile, giving an error about yielding to a function with a void return. Co-routines (as indicated by the yield statement) must return an IEnumerator. The following code does work:

var x : int = 2; //Change this in the inspector

switch(x){ case 1: Debug.Log("x = 1"); yield Function1(); Debug.Log("done"); break; case 2: Debug.Log("x = 2"); yield Function2(); Debug.Log("Done"); break; }

function Function1() : IEnumerator { yield Function3(); Debug.Log("Function1"); }

function Function2() : IEnumerator { yield Function3(); Debug.Log("Function2"); }

function Function3() : IEnumerator { Debug.Log("Function3"); }

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

Answer by Nikolay · Dec 07, 2010 at 10:39 AM

Thank you very much for your answer, mate. Indeed, our design may have considerable room for improvement, however, all those yields have their purpose. And the purpose is that both Function1() and Function2() carry out some checks and, if need be, call Function3() to do some animation. During this time, the mouse must be disabled, so the player cannot tamper with the checks and animations (we couldn't think of a better way to prevent the player from clicking while the animation is on). So, in actuality the switch statement I posted above is a simplified part (NOT the actual code) of an OnMouseDown() function. The idea was to disable the mouse at the beginning of each case (in the above code lines Debug.Log("line 1") and Debug.Log("line 3")), perform the necessary routines (player just watching, or clicking like mad, but unable to wreak any havoc on the game) and re-enable it just before the break statement (lines Debug.Log("line 2") and Debug.Log("line 4") above). I am NOT trying to compile nor run the above code as it is - this was meant to be simply an illustrative example of what we're actually doing. I know that I cannot yield a void function. It must either explicitly, or implicitly return IEnumerator. Function1() and Function2() above contain a yielded call to Function3(), so, to my understanding at least, they do not need the IEnumerator in their signatures (of course, it might be a better programming style to include one). Function3() also contains yielded calls to other functions I didn't mention for brevity. Perhaps I should've :/.

In short, it is imperative that execution should return from Function1() and Function2() (in the example) so the mouse can be unlocked and the game can resume. Function2(), however, still evades us. I would be eternally grateful, if somebody would try and show us a way out of this conundrum.

I use this opportunity to thank Mike for his comment. I've been trying to read some of your other posts as well, hopefully I find a solution. According to the api, however, yield does the same as yield StartCoroutine(), in the WaitAndPrint(2.0) example at least http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html?from=index Is it true?

Thanks,

N.

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

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

No one has followed this question yet.

Related Questions

Yield statement is not working 1 Answer

Problem With Functions and Yield 1 Answer

yield WaitForSeconds preventing further actions. 1 Answer

Why is my delay not working? 2 Answers

Create a class like WWW which a IEnumerator will wait on. 0 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