Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Fewpwew130 · Oct 06, 2014 at 11:57 AM · coroutinetimefor loop

Use for-i-loop var outside the loop

Hello, dear community. I am stuck with a trivial problem: using the value of a variable outside the loop.

Currently "i" seems to be accesible only inside the for-loop. I would like to use its value in the Update and other function.

Since I would like a variable to change every second. Is it possible?

My current script in c#:

 using UnityEngine;
 using System.Collections;
 
 public class time2 : MonoBehaviour {
 public int i;
 
     void Update () {
         StartCoroutine(Stopwatch());
         Debug.Log ("Now i is equal to:");
         Debug.Log (i);
         }
 
     IEnumerator Stopwatch(){
         for (int i = 0; i < 10; i++){
         yield return new WaitForSeconds(1);
         }
     }
 }
 
Comment
Add comment
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

2 Replies

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

Answer by jpthek9 · Oct 06, 2014 at 12:52 PM

 using UnityEngine;
 using System.Collections;
  
 public class time2 : MonoBehaviour {
     public int i;
  
     void Update () {
         StartCoroutine(Stopwatch());
         }
  
     IEnumerator Stopwatch(){
         for (i = 10; i < 20; i++){
             yield return new WaitForSeconds(1);
             Debug.Log (i);

             }
     }
 }

Comment
Add comment · Show 2 · 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 Fewpwew130 · Oct 06, 2014 at 06:24 PM 0
Share

jpthek9, thank you very much!!! It worked!!!

dmg0600, Bunny83, jpthek9, thank you all. I apreciate your help and how quickly you were able to explain me where I was wrong.

avatar image jpthek9 · Oct 08, 2014 at 01:17 AM 0
Share

The main thing was that before, you were doing Debug.log(i) outside of the Coroutine. The value gets changed in the Coroutine so you have to Log in the Coroutine with the same time intervals. Otherwise, you'll just get a flurry of Logs.

avatar image
1

Answer by dmg0600 · Oct 06, 2014 at 12:01 PM

 using UnityEngine;
 using System.Collections;
  
 public class time2 : MonoBehaviour {
 public int i;
  
     void Update () {
         StartCoroutine(Stopwatch());
         Debug.Log ("Now i is equal to:");
         Debug.Log (i);
         }
  
     IEnumerator Stopwatch(){
         for (i = 0; i < 10; i++){
         yield return new WaitForSeconds(1);
         }
     }
 }

Removing the int declaration of the for loop it will use the class variable instead of its own local variable. This way you can see its value outside the loop.

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 Fewpwew130 · Oct 06, 2014 at 12:45 PM 0
Share

dmg0600, thank you for your promt answer!

Unfortunately, I failed to make it work. $$anonymous$$y program still counts "i" as initial declared value.

I want it to show: 10 11 12 13 14 15 16 17 18 19

but what I get is only 10.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class time2 : $$anonymous$$onoBehaviour {
     public int i;
     
     void Update () {
         StartCoroutine(Stopwatch());
         Debug.Log (i + 10);
         }
     
     IEnumerator Stopwatch(){
         for (i = 0; i < 10; i++){
             yield return new WaitForSeconds(1);
             }
     }
 }

avatar image Bunny83 · Oct 06, 2014 at 12:54 PM 1
Share

@Fewpwew130: First of all, plesae don't post replies as answer. Answers should answer the question. I converted your answer into a comment.

Your problem is that you start a new coroutine every frame. Since non of the coroutines can ever exit (since all use the same variable "i") you end up with tons of coroutines running at the same time.

You should start your coroutine just once in Start. It runs automatically in the background.

@dmg0600: You might want to change your answer since you're doing the same in your answer and it doesn't make much sense that way...

avatar image Fewpwew130 · Oct 06, 2014 at 06:22 PM 0
Share

Bunny83, thank you for the explanation!

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

30 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

Related Questions

Run coroutine X amount of times in Y seconds? 3 Answers

How to check if input was made x seconds/frames ago? 0 Answers

stop game for 1 second 2 Answers

If statement not being fullfilled until GameObject inspected in inspector. 0 Answers

Loop not executing all code - for in loop 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