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 ljw92 · Jun 15, 2016 at 06:15 AM · uiguitextvehicles

How do I constantly update a variable that is displayed in a UI text component?

I have a vehicle level that I set up using the Standard Assets vehicle sample level. The CarController script contains a float named Revs that I want to constantly update in the UI while driving around. I have this set up in the CarController script by converting it to a string and assigning it to a text variable which is linked in to the UI. This displays the variable but it only briefly flickers on-screen every so often, how can I make Unity constantly update the variable in the UI so I can always see what the current value is?

alt text

link text

revtext-01.jpg (11.2 kB)
carcontroller.txt (14.1 kB)
Comment
Add comment · Show 1
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 KuR5 · Jun 15, 2016 at 06:55 AM 0
Share

Try out : void Update () { SetRevText(); }

Further you can update the revText via coroutine also.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Brijs · Jun 16, 2016 at 01:35 PM

More efficient way

1) Make Property of Revs variable.

2) In set{} block of property call method to change revsText.text

For example:

 private int revs;
 
     public int Revs
     {
         get { return revs; }
         set
         {
             revs = value;
             ReflectValue(revs);
         }
     }
 public void ReflectValue(int revs)
 {
         revText.text = revs.ToString();
 }

Here you can use event instead of direct method ReflectValue() to have more decoupled and extendible code

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
avatar image
2

Answer by EpiFouloux · Jun 15, 2016 at 07:42 AM

 void Update()
 {
   revtText.text = "RPM: " + value.ToString();
 }
 
 // or
 
 void Start()
 {
  StartCoroutine(Cor_show_level());
 }
 
 private IEnumerator Cor_show_level()
 {
  while (true)
  {
    revtText.text = "RPM: " + value.ToString();
    yield return new WaitForSeconds(time_between_two_prints);
  }
 }

EDIT : I believe that the constant updating idea is not great : You should update the text only when you update the value to reduce computing

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Having a lot of trouble with GUI scaling 1 Answer

Find GUI Button and Assign Text 1 Answer

Having trouble getting text to show up. 0 Answers

UI Text Component being reset on player when built 0 Answers

Cached Text Generators characterCountVisible returning incorrect values 2 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