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
2
Question by Miriam · Apr 04, 2010 at 02:57 PM · coroutinefunctionyield

Coroutines and waiting for a function to finish execution

Hi,

I need to call a function and wait for it to finish before continuing the script.

This is what I have at the moment:

var cardDeck;

function newHand(){

  //do stuff

  yield shuffleCards();

  //do other stuff - that relies on the cardDeck variable

}

function shuffleCards (){

 //do stuff

 //change cardDeck var

 yield; //does this pause the game for a whole frame? can I avoid it??

}

The problem with this is that I don't want to pause the game for a whole frame - just for long enough for the function to finish executing. I am yielding functions quite a few times in this script and it could add up.

I have tried it without the yields and it seems to work fine. - I am just worried that this is messy scripting and could lead to later problems.

I have also tried it without the second yield statement (in the shuffleCards function) and it comes back with the error message: 'It is not possible to evaluate an expression of type void'.

Is there any way I can make sure the function has finished before continuing without pausing for a whole frame?

Thank you in advance.

Yours

Miriam.

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 SgtCode · Jun 14, 2018 at 11:45 AM 0
Share

This stems from 2010. 8 years later I'm stuck with somewhat of the same issue. I'd like to call a coroutine recusively with yield return startCoroutine(function(param)); without yielding the result... Any ideas?

1 Reply

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

Answer by Eric5h5 · Apr 04, 2010 at 04:13 PM

If a function is not a coroutine (doesn't have yield in it), and you call it normally, all the code in the function must be executed before anything else can happen anyway. Unity scripting is single-threaded and can only do one thing at a time, unless you specifically create threads.

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 Miriam · Apr 04, 2010 at 06:34 PM 0
Share

thank you very much!

avatar image Max_Bol · Sep 12, 2017 at 02:42 AM 0
Share

Functions aren't coroutine, but Unity scripting isn't single-threaded either. (Or at least, not anymore it seems.)

Each separate scripts files are kinda like a coroutine. If you call a function in a script from another script, the function might not be finished before the processor move onto the next line in the first script's code.

The time required for the processor to pass through a script isn't big so the risks aren't nearly as present as I may sound, but any actions that requires, for example, the input or output of another component other than the processor can make a relatively huge difference here. For example, calling a function that deserialize a remote file on the HDD might not be finished before the next line of code is processed in a script IF that function isn't present in the said script.

So if you got a script A that call function "SaveCharacter" which exist in Script B and the process involve quite a few data management and/or conversion on the spot (like converting some unserializable data into strings to be serialized), the "SaveCharacter" function might not be done by the time the processor run the next lines of code in the Script A.

I have faced some major issues related to this by having a saving process "halted" mid-way as I called the save process prior to a scene change function. The Save function wasn't done within the time before the change scene was called and the save file was then incomplete and unusable.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to properly use Yield for a coroutine? 3 Answers

How to use yield within a class function 2 Answers

StartCoroutine important for using yield? 1 Answer

Coroutoutine doesnt work properly. 3 Answers

Im a bit confused on a simple script 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