Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ony · May 06, 2010 at 05:58 AM · coroutinefunctionyieldwait

How to properly use Yield for a coroutine?

I have some functions that fire one after the other and I want to make sure the first one is finished before the second one starts.

I used "yield" before the function names, ie:

yield myFirstFunction();
yield mySecondFunction();

Sometimes this works fine, but other times it gives an error:

"It is not possible to evaluate an expression of type 'void'."

If I remove yield from the first line it still errors out.

What's weird is that sometimes this works fine. Even for the block of code. It just stops working when I change something down the line but I don't really know what it is that's breaking it.

Can anyone shed some light on this? I searched the forum but found nothing. The errors in question are happening in a Javascript script.

ADDED: Here is one of the functions that won't let me yield it:

function FillCharactersThumbArray() { // loads in thumbnail images from a local folder and puts them into the appropriate character thumb in the listCharacters[] array...

 for (nng=0;nng<listCharacters.length;nng++)
 {
     var www1 = new WWW ("file:///c:/SS/AssetPacks/thumbs/c/tn_"+listCharacters[nng].gender+"_"+listCharacters[nng].image+".png");
     yield www1;

     listCharacters[nng].image = www1.texture;
 }   

}

Comment
Add comment · Show 2
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 Eric5h5 · May 06, 2010 at 06:09 AM 0
Share

When you use file:// in WWW you don't need to yield anyway (it gets the result immediately), so that doesn't have to be a coroutine at all.

avatar image Ludovic · Nov 23, 2010 at 01:57 PM 0
Share

Well, you need to access to the file. If it's a big one or there are multiple files, it can freeze your game. Whether it's bearable or not depends of the context, e.g. when you're loading a level or you're about to snipe the ennemy ;)

3 Replies

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

Answer by Mike 3 · May 06, 2010 at 06:01 AM

the function that you call needs to be of type IEnumerator - this is done implicitly when you use yield in the function, but if you add in a return (or it is possible to get through the function without hitting a yield), it'll see it as a void function

if you want to tell the compiler to do it explicitly, you could do function myFirstFunction() : IEnumerator {

Edit - In the example you posted, the compiler can't be sure there will be a yield if the string is too short (0 chars) - so it's confused as to the return type

you could fix it by putting yield; at the end, or making the function IEnumerator

Comment
Add comment · Show 1 · 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 Ony · May 06, 2010 at 06:14 AM 0
Share

Thanks $$anonymous$$ike and Eric. I'm still wrapping my head around it but your answers are going a long way towards helping me understand.

avatar image
0

Answer by Absyss · Aug 09, 2010 at 10:15 AM

what Mike meant is following:

function myFirstFunction():IEnumerator { ... } function mySecondFunction():IEnumerator { ... }

...

yield myFirstFunction(); yield mySecondFunction();

this way the compiler wont get confused about the correct type.

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 Techsuport · Sep 02, 2011 at 09:29 AM

you could do something like a call back, so even though you cant get the return right away, you can have function B call function D in the same class as function A to continue on where A left off.

    class Face{
        var brain:Brain;
        function Talk(){
           brain.Think(OfWhatToSay)
        }
        function OfWhatToSay(words:String){
          print(words)
        }
     }
     
     class Brain{
        function Think(callBack:function(String)){
           callBack(GetWordsToSayElseWhereInThisClass());
        }
        function GetWordsToSayElseWhereInThisClass():String{
           return "Giberish";
        }
     }

check out this for an intro for callbacks.

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

1 Person is following this question.

avatar image

Related Questions

WaitForSeconds Question 2 Answers

how would i cause the function to wait until it stops to function again? 1 Answer

C# yield not working. 2 Answers

Yield And Return 2 Answers

Best way to wait for event inside Coroutine? 3 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