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 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.

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

Is there any way to stop WaitForSeconds() ? 1 Answer

Can't call StopCoroutine function from another script. 1 Answer

If I StopCoroutine("myCoroutine")', will the variable values in myCoroutine be reset? 1 Answer

How to force Coroutine to finish 1 Answer

Coroutine problem 0 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