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 superjoy0502 · Aug 04, 2018 at 09:26 AM · unityeditorcoroutineienumeratorwhile loop

Problems with While Loops Only repeating Once

Hello, a fellow Unity use having time developing a basic clicker game, And having an error of course! I'm having this while loop in this coroutine, but it's only repeating once! It only running when the game starts, and here's the edited Script:

     private void Start()
     {
         DataController.Instance.LoadItemButton(this);
         StartCoroutine("AddGoldLoop");
 
         UpdateUI();
     }
     IEnumerator AddGoldLoop()
     {
         Debug.Log("AddGoldLoop");
         while (true)
         {
             Debug.Log("WHILE");
             if (isPurchased)
             {
                 Debug.Log("ISPURCHASED");
                 DataController.Instance.gold += goldPerSecond;
                 StartCoroutine ("BG");
             }
 
             Debug.Log("IFEND");
 
             yield return new WaitForSeconds(1.0f);
             
         }
     }
 

There is a problem in "AddGoldLoop()" and I can't find out why. Thanks for Answering my question!

Comment
Add comment · Show 2
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 Stratosome · Aug 04, 2018 at 10:05 AM 0
Share

Hmm, that's odd. I tried the code and commented out the stuff I don't have, but the loop works for me. Try reducing the AddGoldLoop to just the base mechanics and see if that works? Like this:

 IEnumerator AddGoldLoop() {
     while (true) {
         Debug.Log("Loop iteration");
         yield return new WaitForSeconds(1.0f);
     }
 }

If this doesn't loop I'd be surprised.

avatar image khooroko · Aug 04, 2018 at 10:10 AM 0
Share

See if the answers here https://answers.unity.com/questions/341663/problem-with-while-loop-in-coroutine.html can help you

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GregoryFenn · Aug 04, 2018 at 10:38 AM

What does your BG coroutine do? Maybe that is setting isPurchased to false?

As a debug tip, can you add a counter for while loop, and print out Debug.Log(isPurchased) before and after? e.g. write this after your Start(){} method:

 int c = 0;
 
 IEnumerator AddGoldLoop()
      {
          c++;
          Debug.Log(isPurchased.ToString() + ": " + c.ToString());
          Debug.Log("AddGoldLoop");
          while (true)
          {
              Debug.Log("WHILE");
              c++;
              Debug.Log(isPurchased.ToString() + ": " + c.ToString());
              if (isPurchased)
              {
                  Debug.Log("ISPURCHASED");
                  DataController.Instance.gold += goldPerSecond;
                  StartCoroutine ("BG");
                  c++;
                  Debug.Log(isPurchased.ToString() + ": " + c.ToString());
              }
  
              Debug.Log("IFEND");
              c++;
              Debug.Log(isPurchased.ToString() + ": " + c.ToString());
  
              yield return new WaitForSeconds(1.0f);
              
          }
      }



This should help us find the issue, but I suspect its with your BG coroutine.

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 superjoy0502 · Aug 04, 2018 at 12:29 PM 0
Share

@GregoryFenn

     IEnumerator BG()
     {
         Debug.Log("BG");
 
         I$$anonymous$$G.SetActive (true);
         yield return new WaitForSeconds (.2f);
         I$$anonymous$$G.SetActive (false);
 
     }

It does this kinda things, to change the background image.

avatar image superjoy0502 · Aug 04, 2018 at 01:42 PM 0
Share

Ok, so, the 'c' only Logs 3 times...

avatar image superjoy0502 superjoy0502 · Aug 04, 2018 at 01:49 PM 0
Share

AND... 4 times...

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

97 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

Related Questions

Randomly changing between two tags using coroutine 2 Answers

Problem with while loop in Coroutine 2 Answers

Return result of the IEnumerator. 2 Answers

Make a function that can return string or IEnumerator 2 Answers

How to pass "ref parameter" in iterator method? 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