Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 alexhapki · Sep 11, 2015 at 08:17 PM · unity 5coroutinefreeze

Coroutine called in different instances

hi everyone,

Just curious how coroutines work. I read some threads about the matter but there is still something it is not clear to me. I have multiples enemies, for example 4 enemies, each of them uses a script called EnemyAI and it is attached to the object (enemy), which has a coroutine for Idle, IdleInCombat, Move, MoveInCombat, Attack, etc. Now, the coroutine is called FSM. Question is, what happens really?;

Option 1) Each script of each enemy, calls for coroutine called FSM, it means 5 coroutines work at the same time.

Option 2) Somehow the 5 corutines can interfere each other?

I guess it is option 1),but I am not sure, I am having a lot of troubles and unity editor completely freezing. I even Split the coroutine to two coroutine (one InCombat states and another NonInCombat states), it helped to reduce the issues, but it ends freezing Unity. It worked perfectly when the coroutine was simple with only Idle, Move, ...Just checking if the same name of a coroutine could interfere other objects using instances of the same script.

Thank you in advance,

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 Dave-Carlile · Sep 11, 2015 at 08:20 PM

Coroutines are a form of cooperative multitasking. A coroutine will run until it reaches a yield statement, then the next one will run until it reaches a yield, and so on. They are not running at the same time, and they are all running on the main thread so there are no synchronization issues to worry about.

If a coroutine does too much work before yielding then it that can cause issues like what you're seeing. For example if you have something that loops forever and you never yield inside that loop then it will never finish, and since it's running on the main thread then the Unity editor will lock up.

Comment
Add comment · Show 5 · 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 alexhapki · Sep 11, 2015 at 09:00 PM 0
Share

Hi Dave, Thanks for the fast reply. Then if I have one script (EnemyAI) with a coroutine attached to 5 objects, there will be 5 coroutine called by the same name, working normally, but they wont interfere each other. is that Correct?

Thank you in advance

avatar image Dave-Carlile alexhapki · Sep 11, 2015 at 09:03 PM 0
Share

They won't interfere with each other in a threading sense because they all run on the same thread, one after another. But again, if once of them runs a long time without calling yield then it will interfere with everything else running on the main thread, including delaying the other coroutines.

avatar image Bunny83 · Sep 11, 2015 at 09:10 PM 1
Share

@alexhapki:
Yes, those coroutines never run at the same time. The coroutines run after each other. At no point in time code from two coroutines will run at the same time. Parts of each coroutine will run after each other. For example if your coroutines all have a yield return null; in a loop, each coroutine will execute one loop iteration each frame. They will be executed just like Update.

avatar image alexhapki Bunny83 · Sep 12, 2015 at 12:27 PM 0
Share

Thanks for the clarifation Bunny83. I have a much clearer idea now with all the comments from you and Dave.

Ps. I am trying removing Update() functions as much as possible from Scripts, coroutines is one of the tools seem to help with that.

avatar image Bunny83 alexhapki · Sep 12, 2015 at 01:17 PM 0
Share

Why do you want to remove Update functions? Coroutines, when used like update have slightly more overhead. They make sense if you want / need to interrupt the execution or if you need certain things to happen "in order".

avatar image
0

Answer by alexhapki · Sep 14, 2015 at 10:08 AM

I dont have a lot of experience at improving scripting efficiencies, I just follow experienced people advice like yours and others, in general I got the idea of using "if statements" for simple checks that might not happen in order. Anything that is being called at all times but only one option can be active, coroutine could be better, for example I am using this corutine for enemy AI, there are a number of finite states, and only one can be active at once; Idle, Move, Attack, Flee, etc. So it does not use continously if, else if, else if, etc. As you mentioned, things happen in order in my AI Script.

on the other hand, for example, in my project I use " if statements" for input keyboard/mouse to control main carácter, . I consider it "simple", no things that happens in order and any imput can happen at anytime.

Regarding efficiency, I try keeping an eye on all comments experienced folk do and learn as much as I can. I also read with interested the following link even though it might be a bit up of date; http://wiki.unity3d.com/index.php?title=General_Performance_Tips

Thanks

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

am trying to scaling my ground size in z direction until my player is moving, 2 Answers

Protection against intersection of generated objects. 0 Answers

Unity fails when i press play 0 Answers

Coroutines randomly stop working 0 Answers

Startcoroutine not working second time 2 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