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 IgorUnity3D · Jul 24, 2016 at 08:41 AM · coroutineclasseventcoroutinessingleton

StartCoroutine by another class (Coroutine inside Coroutine - Instance class)

Hi, we have this class:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class ScreenManager : MonoBehaviour {
     
 
     [SerializeField]
     private EFXManager m_blackScreenCover;
 
     [SerializeField]
     private EFXManager m_form;
 
     [SerializeField]
     private float m_minDuration = 1.5f;
 
     public static ScreenManager instance = null;
 
     void Awake()
     {
         if (instance == null)
             instance = this;
 
         else if (instance != this)
             Destroy(gameObject);
 
         DontDestroyOnLoad(gameObject);
     }
 
     void Update()
     {
         //if (Input.GetMouseButtonDown(0))
         //{
         //    StartCoroutine(LoadSceneAsyncScale("GameScreen"));
         //}
     }
 
     public IEnumerator LoadSceneAsyncScale(string sceneName)
     {
         // Scale IN
         yield return StartCoroutine(m_form.ScaleIn());
 
         // Load loading screen
         yield return SceneManager.LoadSceneAsync("LoadingScreen");
 
         // !!! unload old screen (automatic)
 
         // Fade to loading screen
         yield return StartCoroutine(m_blackScreenCover.FadeOut());
 
         float endTime = Time.time + m_minDuration;
 
         yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
 
         while (Time.time < endTime)
             yield return null;
 
         // Fade to black
         yield return StartCoroutine(m_blackScreenCover.FadeIn());
 
         // !!! unload loading screen
         LoadingSceneManager.UnloadLoadingScene();
 
         // Scale to new screen
         yield return StartCoroutine(m_form.ScaleOut());
     }
 
 }

We are calling the method LoadSceneAsyncScale through another class. Everything seems to go well, but the method in question have more Coroutines ( yield return StartCoroutine (m_blackScreenCover.FadeOut (); for example ) and we noticed that it does not run. The implementation of the method will only LoadSceneAsyncScale to call yield return SceneManager.LoadSceneAsync ("LoadingScreen"); What could be going wrong? The call is incorrect?

My call:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;

 public class EventManager :  MonoBehaviour
 {
     public void OnButtonClick()
     {
         StartCoroutine(ScreenManager.instance.LoadSceneAsyncScale("GameScreen")); //<< THIS IS A CALL, DON'T WORKS!
     }
 }

But when we called directly from the class that has the method, in the Update () function could perform all routines:

 public class ScreenManager : MonoBehaviour {
    //........
   void Update()
   {
     StartCoroutine(LoadSceneAsyncScale("GameScreen")); //<< THIS WORKS FINE!
   }
    //........
 }


What are we doing wrong? Remembering that we are using the Singleton pattern.

Thanks a lot!

[SOLUTION]

With the help of the @Bunny83 response, we can implement the Singleton pattern in the class that will call the coroutine ( EventManager ) so the calling object will not be destroyed! Or just do what the @Bunny83 said in your answer!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class EventManager : MonoBehaviour
 {
    public static EventManager instance = null;
 
     void Awake()
     {
         if (instance == null)
             instance = this;
 
         else if (instance != this)
             Destroy(gameObject);
 
         DontDestroyOnLoad(gameObject);
     }
 
     public void OnButtonClick()
     {
         StartCoroutine(ScreenManager.instance.LoadSceneAsyncScale("GameScreen"));
     }
 }

Thanks @Bunny83 ;)

Comment
Add comment · Show 4
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 Cherno · Jul 24, 2016 at 12:41 PM 0
Share

$$anonymous$$aybe try it without the yield return before it, or have a line with yield return null line before starting each Coroutine.

avatar image IgorUnity3D Cherno · Jul 24, 2016 at 04:51 PM 0
Share

Hi @Cherno! It does not work :\

avatar image Cherno IgorUnity3D · Jul 24, 2016 at 05:17 PM 0
Share

Well, too bad :(

A workaorund would be to make the CR call seperate (normal) functions that in turn start the respective CoRoutines.

Show more comments

1 Reply

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

Answer by Bunny83 · Jul 24, 2016 at 05:32 PM

You run your outer coroutine on the "EventManager" script because you used the EventManager's StartCoroutine method to start your coroutine. That will start the coroutine on the EventManager. If the EventManager is destroyed the coroutine will be gone. Try this

 public void OnButtonClick()
 {
     ScreenManager.instance.StartCoroutine(ScreenManager.instance.LoadSceneAsyncScale("GameScreen"));
 }

This will make the coroutine to run on your singleton instance which will not be destroyed due to the DontDestroyOnLoad.

It's usually a better approach to implement a normal wrapper method inside your ScreenManager which starts the Coroutine locally:

 // inside SceneManager
 public void StartLoadSceneAsyncScale(string sceneName)
 {
     StartCoroutine(LoadSceneAsyncScale(sceneName));
 }

And then use it like that:

 //Inside EventManager
 public void OnButtonClick()
 {
     ScreenManager.instance.StartLoadSceneAsyncScale("GameScreen");
 }


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 IgorUnity3D · Jul 25, 2016 at 02:19 AM 0
Share

Thank you very much for your reply @Bunny83! It really works very well ... I did not know that a coroutine was destroyed along with the object. All you also said works by implementing Singleton in Event$$anonymous$$anager. :) I update the question with solution!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change this using a coroutine 1 Answer

Waiting for and using output of a UnityEvent within coroutines 1 Answer

I need to Stop and Restart a Co-Routine at anytime 1 Answer

Nested Coroutines: last coroutine quits early 1 Answer

Coroutine is jerky after exiting trigger zone 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