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 SaltwaterDevil · Jul 17, 2019 at 06:04 PM · uitextupdate

Text UI not updating

Hi! I'm new to Unity and C# and working on a project. I want to make an on-screen timer tick down as the player moves, but for some reason the text on screen isn't updating. Here's the script I've put on the text object, which I have as the child of a UI canvas:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class MovementUI : MonoBehaviour
 {
     //Script changes UI text to display value of MovementTimer.movementTimer
 
     float movementMeter;
     Text display;
 
     // Start is called before the first frame update
     void Start()
     {
         display = GetComponent<Text>();
         movementMeter = GameObject.Find("PlayerCircle").GetComponent<MovementTimer>().movementTimer;
         Debug.Log(movementMeter);
     }
 
     // Update is called once per frame
     void Update()
     {
         display.text = "" + movementMeter;
     }
 }

You can see that the float movementMeter is initialized to a float from another script. This is working correctly, Debug.Log(movementMeter) in the Start function prints the correct number to the console. I'm guessing it's a problem with how I'm using display.text, but I'm not sure.

Thank you!

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
0
Best Answer

Answer by k234234w · Jul 17, 2019 at 06:23 PM

movementMeter will get set once in the start, I am not seeing where you are having it tick down? Is the number you are seeing displayed different from what is logged in start? From what I am seeing your are not altering movementMeter so it will be the same every frame, and thus the text will not change. Initializing it to the float from the other script doesnt mean every time it changes in the other script you will see it in your update change. You have a local float variable, you are setting its value once and only once.

Comment
Add comment · Show 4 · 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 k234234w · Jul 17, 2019 at 06:27 PM 0
Share

You could get a reference to $$anonymous$$ovementTimer, cache it. And if you make $$anonymous$$ovementTimers movementTimer public, you could set your text to that in Update.

 public class $$anonymous$$ovementUI : $$anonymous$$onoBehaviour
  {
      //Script changes UI text to display value of $$anonymous$$ovementTimer.movementTimer
  
      private $$anonymous$$ovementTimer movementTimer = null;
      Text display;
      
     private void Awake()
     {
         movementTimer = GameObject.Find("PlayerCircle").GetComponent<$$anonymous$$ovementTimer>();
     }
      // Start is called before the first frame update
      void Start()
      {
          display = GetComponent<Text>();
      }
  
      // Update is called once per frame
      void Update()
      {
          // Assu$$anonymous$$g its a public variable.
          display.text = "" + movementTimer.movementTimer;
      }
  }
avatar image SaltwaterDevil · Jul 17, 2019 at 06:57 PM 0
Share

You're right, I guess I assumed movement$$anonymous$$eter would update alongside $$anonymous$$ovementTimer.movementTimer. I moved the line where I define movement$$anonymous$$eter from Start() to Update(), and it works fine now. Thank you!

avatar image k234234w SaltwaterDevil · Jul 17, 2019 at 07:00 PM 0
Share

It would be better to not have to do the GameObject.Find every update frame. Why can't you just get a reference to $$anonymous$$ovementTimer in Awake, and then in your update just set the text to the cached reference to $$anonymous$$ovementTimers movementTimer variable.

avatar image k234234w k234234w · Jul 17, 2019 at 07:02 PM 0
Share

Try out my code ins$$anonymous$$d of using GameObject.Find every update frame.

avatar image
0

Answer by KloverGames · Jul 17, 2019 at 06:29 PM

Make sure you have an event system in your hierarchy, I know you need it for buttons but maybe it's for all ui updates

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

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

Text Not updating 1 Answer

uGUI Text not updated until RectTransform has been altered 1 Answer

Text component changes the text but doesn't remove the previous version. 0 Answers

Multiple gui instantiation issues 0 Answers

How to add string once to the UI text, but update the substring every frame in unity? 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