Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
2
Question by Aubrey-Falconer · Dec 19, 2011 at 08:21 AM · coroutineloadlevelthreads

Coroutine Death on Scene Change

I am writing a Unity library to load game worlds over the internet. It uses coroutines (and thus needs to extend MonoBehaviour and attach to a GameObject in the scene), and it also needs to be able to keep working during scene changes (I need to start downloading a world in one scene, then load another scene and begin generating the world).

Currently works like this:

(1) Code instructs singleton World Loader library to initialize. (2) Library creates a GameObject, marks it as DontDestroyOnLoad, attaches itself to it (so it can use coroutines), and points a static reference to the instance of itself it created. (3) Code calls load coroutine in Loader library. (4) Code waits a bit, then switches to a new Unity scene. (5) From what I can tell, switching scenes causes the coroutine to instantly and silently die in the middle of it's work.

I am assuming that even though the Library's GameObject is marked as DontDestroyOnLoad, the script attached to it is being reloaded on scene change (And indeed, it's OnDisable and OnEnable functions are being called). I don't know why this would happen.

Questions:

(1) Are coroutines supposed to die on scene changes, or am I doing something wrong?

(2) I could add some OnEnable logic to detect when the script had been reloaded, and fire up the coroutine again (which would involve more logic to figure out what step it was at when it died), but it would be terribly inelegant and a potential source of future bugs. Should I go this route, or would it make more sense to pursue some form of system level threading rather than trying to work with Unity's coroutines? (Is there a solution which would make more sense?)

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

3 Replies

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

Answer by dannyskim · Dec 19, 2011 at 08:32 AM

  1. Yes, co-routines as you've confirmed yourself are supposed to die on scene change, even though ultimately this doesn't make sense especially with dontdestroyonload.

  2. I've run into this problem before myself, and ended up using OnEnable and OnDisable to save variable states between scenes loading, which I guess is the direction that you may have been headed in, and to even keep track of time between the co-routine being stopped and resumed between the level loading states.

In the end, I actually did end up using some lightweight threading to get what I needed to be done, done. It my mind it made sense to do it this way because it ultimately felt more elegant, but some people may not want to go this route because they'd rather not have to add more base overhead to their project by using more library namespaces. I personally think it makes sense, but I am an amateur and can't give you a definitive answer.

Comment
Add comment · Show 3 · 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 Aubrey-Falconer · Dec 20, 2011 at 07:20 PM 0
Share

Thanks Danny. $$anonymous$$y library relies rather heavily on Unity's libraries, so I guess threading isn't an option for me. I just wrote a system to keep track of where my coroutine is at and relaunch it if necessary, and this works fine. It is too bad I had to at all...

avatar image dannyskim · Dec 20, 2011 at 07:38 PM 0
Share

I agree that it's kind of odd that Unity didn't take this into though, but you may want to consider submitting this as an option request. If enough people vote for it, then they may actually work on it.

I don't find adding the threading namespace all that much trouble, you may want to consider trying it.

avatar image PuzzleBuilder · Apr 22 at 04:55 PM 0
Share

I believe this is no longer the case. I'm using Unity 2019.4 and coroutines appear to keep running after the scene changes on objects where I've called DontDestroyOnLoad. It's hard to find documentation on this, so hopefully it's a reliable behavior. I'm thinking that these objects used to be disabled and re-enabled when the scene changed, but that doesn't appear to be the case anymore.

avatar image
0

Answer by Streamfall · Jun 15, 2014 at 04:36 AM

 Heres a way around this : 
 
 void Go()
 {
 Application.LoadLevel(1);
 BeginChain();
 }
 
 void BeginChain()
 {
 StartCoroutine(Chain());
 }
 
 public IEnumerator Chain()
 {
 while(Application.isLoadinglevel)
 {
 yield return null;
 }
 
 //do some stuff after loading
 }
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 PuzzleBuilder · Apr 22 at 04:57 PM

I believe this issue was resolved in newer versions of Unity. My coroutines continue to run when the scene changes on objects with DontDestroyOnLoad.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Crash using Sockets 2 Answers

Threading in Unity 6 Answers

Coroutine only starts when directly loading a scene from Unity editor. 1 Answer

Wait For Seconds to Load level C# 2 Answers

How to handle a slow function from a dll 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