• 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 unity_0sdsWZad6Fwp-A · Jan 19, 2019 at 03:09 AM · coroutinehowscene-changestopcoroutinefrozen

How to fix a stuck Coroutine / how to have a coroutine running while another scene is open

I am making a clicker game and whenever I go to a different scene using SceneManager.LoadScene(2); and come back to the other scene the coroutine gets stuck and isn't fixed until it gets updated. Anyone know a way on how to have the coroutine fix itself or know a way to have the coroutine run itself while another scene is open? I have tested and the coroutine doesn't stop with the console but it does in the in game scripts (I have just started using the Scene loading function and I am still getting used to it) Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class AutoSell : MonoBehaviour
 {
 
     public bool SellingOre = true;
     public static float CashIncrease = 1;
     public float InternalIncrease;
 
 
 
 
 
 
     void Update()
     {
 
         CashIncrease = GlobalShop.shopPerSec;
         InternalIncrease = CashIncrease;
 
         
             if (SellingOre == false)
             {
                 SellingOre = true;
                 StartCoroutine(SellTheOre());
 
             }
         
     }
     IEnumerator SellTheOre()
     {
       
         {
 
         }
 
 
         if (GlobalOres.OreCount < InternalIncrease)
         {
             SellingOre = true;
             GlobalOres.OreCount = GlobalShop.shopPerSec;
         }
         else
             GlobalCash.CashCount += InternalIncrease;
         GlobalOres.OreCount -= GlobalShop.shopPerSec;
         yield return new WaitForSeconds(1);
         SellingOre = false;
 
         
 
 
         if (GlobalOres.OreCount < GlobalShop.shopPerSec)
         {
             if (GlobalOres.OreCount >= 1)
            {
                GlobalCash.CashCount += GlobalOres.OreCount;
                GlobalOres.OreCount -= GlobalOres.OreCount;
 
 
             }
       }
 
 
 
 
 
 
     }
 }

and

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AutoOre : MonoBehaviour {
 
     public bool CreatingOre = false;
     public static float OreIncrease = 1;
     public float InternalIncrease;
 
 
     void Update () {
         OreIncrease = GlobalMiner.MinePerSec;
         InternalIncrease = OreIncrease;
       
         
             if (CreatingOre == false)
             {
                 CreatingOre = true;
                 StartCoroutine(CreateTheOre());
             }
         
     }
 
     IEnumerator CreateTheOre()
     {
         GlobalOres.OreCount += InternalIncrease;
         yield return new WaitForSeconds(1);
         CreatingOre = false;
       
     }
 }
 
 
 

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 haruna9x · Jan 19, 2019 at 03:50 AM 0
Share

Try use Scene$$anonymous$$anager.LoadSceneAsync.

avatar image unity_0sdsWZad6Fwp-A haruna9x · Jan 19, 2019 at 04:13 AM 0
Share

I tried using Scene$$anonymous$$anager.LoadSceneAsync, but the same thing happens.

avatar image haruna9x unity_0sdsWZad6Fwp-A · Jan 19, 2019 at 06:26 AM 0
Share

You need to make sure that the script persists after the new scene has been loaded, ie it has been "DontDestroyOnLoad" affixed. When loading scene, the drop in frame rate should be expected.

Show more comments

1 Reply

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

Answer by unity_0sdsWZad6Fwp-A · Jan 19, 2019 at 08:36 PM

Thankyou @haruna9x for the recommendation. I have fixed the issue with some code I had found in a video about serialization. I just made a public static field then put this code under the script.

 if (check == null)
         {
             DontDestroyOnLoad(gameObject);
             check = this;
         }
         else if (check != this)
         {
             Destroy(gameObject);
         }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

107 People are following this question.

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

Related Questions

NullReference exception after StopCoroutine 2 Answers

Item duration doesn't stack 3 Answers

I'm stopping coroutines in pause but can't bring them back 0 Answers

StartCoroutine with string method name from another script? 1 Answer

How to stop coroutine with parameters? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges