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 kromenak · Aug 31, 2011 at 02:11 AM · coroutinelevelloadlevel

Running Coroutines Between Levels

Hey everyone,

Ran into an issue with coroutines, hopefully I can get some idea if what I'm trying to do is possible.

I have a coroutine that plays a sequence of audio clips. It will play one, yield until it completes, and then play the next, and then ultimately loop the sequence.

The problem I'm having is that it seems like changing the level while the coroutine is yielding, waiting for the track to finish, seems to cause the coroutine to stop functioning properly. I can receive no debug messages from the coroutine, and the music stops playing instead of continuing. This also seems to crash the editor, and produce an error code "m_instance == 0" - not much to go on with that message.

Has anyone ran into similar issues, found workarounds, have a way I can accomplish this sort of functionality?

Comment
Add comment · Show 6
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 Peter G · Aug 31, 2011 at 02:28 AM 0
Share

Have you called DontDestroyOnLoad() on the game object that manages audio?

avatar image kromenak · Aug 31, 2011 at 04:39 PM 0
Share

Ah sorry, forgot to mention it. DontDestroyOnLoad is called on the game object, so it does persist between scenes.

avatar image Ray-Pendergraph · Aug 31, 2011 at 07:47 PM 0
Share

Well the source, the listener and the thing playing the sound (if it's a coroutine) have to be DontDestroyOnLoad. It just hit to me that you were talking about sound... splashes are a little simpler. Not even sure how that would work with the listener. http://unity3d.com/support/documentation/Components/class-AudioListener.html

avatar image kromenak · Aug 31, 2011 at 07:59 PM 0
Share

Hmm, well, I haven't made the audio sources and the listener DontDestroyOnLoad, but they are children of the $$anonymous$$anager class, so I assumed that children of objects that are DontDestroyOnLoad would also be DontDestroyOnLoad. Is that not the case?

avatar image Ray-Pendergraph · Aug 31, 2011 at 10:30 PM 0
Share

Well, sounds like you have taken all the proper steps. According to the docs anything under a DDOLed object will not be destroyed either... I suppose you could verify in the editor. I know that loading a scene causes components to temporarily become disabled momentarily even if they are DDOL this may cause some interruption on the Source. Usually if I DDOL the Listener it starts spam$$anonymous$$g the log as there can only be one... this transition from from the listener in one scene to the listener in another might be the culprit. If you figure out what's going on please post back, this is an interesting problem.

Show more comments

4 Replies

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

Answer by kromenak · Sep 08, 2011 at 05:26 PM

Ultimately, running a coroutine between levels like this did not work out for my needs. There was some very odd behavior where it seemed to work some times, but not the others.

What I ended up doing was using the MonoBehavior's Update method rather than a coroutine to get around the problem all together. Since the MonoBehavior was DontDestroyOnLoad, class variables persisted between levels.

I then took advantage of OnDisable and OnEnable to compensate for variable discrepancies between levels. For example, I had a variable that was counting down the time before the current music track was over and should switch to the next music track. Since music continues playing between levels, this timer gets out of sync with the music time. I used OnEnable to resync this variable using AudioSource.time.

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
2

Answer by Ray-Pendergraph · Aug 31, 2011 at 11:26 AM

Yeah, I am pretty sure that we have run into this... it just stops working. Coroutines are ultimately tied to a MonoBehaviour at some point. When you do a regular scene load (not an additive scene) all the game objects not marked DontDestroyOnLoad are wiped out. Along with them their MonoBehaviours and therefore their coroutines.

We usually keep a MonoBehaviour in the game that is accessible as a singleton and is DDOL. This way we can launch Coroutines for tasks like audio clips and intra-scene splash animations from anywhere in the code without having to have a MonoBehaviour or dummy game object and it fixes the odd behavior you mentioned.

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 Gotal_D · Nov 27, 2018 at 10:46 AM

You can find the button by name, get the Button component off of it and invoke an event. I use this for reloading and starting my game.

  public IEnumerator Reload()
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex);
         yield return new WaitForSeconds(1f);
         GameObject.Find("/StartScreenCanvas/StartPanel/StartGameButton/StartGame").GetComponent<Button>().onClick.Invoke();
     } 

This is a really dirty way of doing it but it gets the job done. Hope it helps (after 7 years =D)

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 Ziplock9000 · Dec 07, 2020 at 05:18 AM

It's because your co-routine code goes out of scope and is unloaded when you load in other scenes.

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

Loading Level 1 Answer

"Reset Level", Application.LoadLevel 1 Answer

Use the GUITexture to go to the next scene 1 Answer

Move my entire player gameobject in the next scene (using loadlevel) 1 Answer

Load Level on Collision 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