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 rpl oye · Jun 25, 2010 at 01:32 AM · coroutine

Increment variable over time

hi. I have this small problem to do some automatic update on a guitext. what i want to do is, to update a guitext after 3 seconds constantly until the game finish. the value would be incrementing one by one after 3 seconds.

Class guitextautoupdate{ private int _i; System.Collections.IEnumerator yieldConnect() { _i++; Guitext.text = _i.ToString(); yield return new WaitForSeconds(3); }

 void Start()
 {
     StartCoroutine(yieldConnect( )); 
 }

}

What i think is, this code will update the text after 3 seconds. but it doesnt do it.

so how do i do that?

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

4 Replies

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

Answer by Eric5h5 · Jun 25, 2010 at 01:56 AM

I generally find InvokeRepeating best for this sort of thing.

private int _i = 0; void YieldConnect() { guiText.text = (++_i).ToString(); }

void Start() { InvokeRepeating ("YieldConnect", 3.0f, 3.0f); }

Use CancelInvoke to make it stop. Note that the correct spelling is guiText, not Guitext.

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 rpl oye · Jun 25, 2010 at 02:06 AM 0
Share

thanks dude. this one works.

avatar image
0
Best Answer

Answer by Mike 3 · Jun 25, 2010 at 01:35 AM

Something like this should do it:

Class guitextautoupdate{ private int _i; System.Collections.IEnumerator yieldConnect() { while(true) { _i++; Guitext.text = _i.ToString(); yield return new WaitForSeconds(3); } }

 void Start()
 {
     StartCoroutine(yieldConnect( )); 
 }

}

Comment
Add comment · Show 5 · 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 rpl oye · Jun 25, 2010 at 01:50 AM 0
Share

I did this, but it still doesnt do it. do you think i have to put it inside an update method?

avatar image Mike 3 · Jun 25, 2010 at 01:54 AM 0
Share

definitely not, no. are you sure Guitext.text is right there? Guitext isn't declared, and isn't the normal way to access a GUI Text object

avatar image rpl oye · Jun 25, 2010 at 02:07 AM 0
Share

i use the post from Eric5h5 below. it works.

avatar image rpl oye · Jun 25, 2010 at 02:16 AM 0
Share

sorry $$anonymous$$ike. your post is actually works. I dont know why, i use a function to execute on those coroutine, and it just never go to the next loop. but anyway, we have 2 solutions.

avatar image Mike 3 · Jun 25, 2010 at 09:04 AM 0
Share

I'd go with his too :D

avatar image
-1

Answer by Bob 3 · Nov 25, 2010 at 04:58 AM

How can one do this in javascript for Unity? I've tried, but as a novice coder, translating c# to JS is a little out of my league somewhat. Thanks.

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 Eric5h5 · Nov 25, 2010 at 08:21 AM 0
Share

This isn't an answer...ask this as a new question. I'd answer here anyway, but I can't post formatted code in a comment.

avatar image
0

Answer by qJake · Jun 25, 2010 at 01:36 AM

You just need to make your coroutine have a while loop if you want it to go forever:

IEnumerator yieldConnect()
{
     while(true)
     {
          // your code
          yield return new WaitForSeconds(3);
     }
}

You can also use a condition, like while(i < 10) or something like that. You can also break out of the while loop (and thus, end the coroutine), with the break; keyword.

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

No one has followed this question yet.

Related Questions

Help returning a variable from this script 1 Answer

Coroutine not starting 1 Answer

WebSocketSharp event not spawning sprite 2 Answers

Passing a rectangle as a parameter seems to have no effect on original using StartCoRoutine 1 Answer

Instantiate + 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