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 businesswithchristiansheehan · Dec 21, 2020 at 09:00 PM · uitextvariablestringfloat

Text UI is not being assigned

I'm new to Unity and to coding in general and I was creating a speedrunning game and I wanted the player's best time to show up on screen.

The code I'm using right now runs and doesn't have any errors but neither the Best Time text nor the Best variable is updating.

I have a strong feeling that this has something to do with the variable types. I had to create a new variable "e" to convert a string value to a float value but even though it works, I think it might still be the problem since the text and variable seem to refuse to be assigned to "e".

Anyway, this is my code and I hope someone can help me out. Thank you! using System.Collections; using UnityEngine.UI; using System.Collections.Generic; using UnityEngine;

 public class Timer : MonoBehaviour
 {
     private float seconds;
     public Text timerText;
     private float startTime;
     private bool finished = false;
     public bool Begin2;
     public Text bestTime2;
     public float bestTime = 999;
     public float e;
     // Start is called before the first frame update
     void Start()
     {
         startTime = Time.time;
         Begin2 = false;
         timerText.text = "0.00";
     }
 
     // Update is called once per frame
     void Update()
     {
 
 
 
 
 
 
         if(Begin2){
         if(finished){
             return;
         }
         float t = Time.time - startTime;
 
         string minutes = ((int) t / 60).ToString();
         string seconds = (t % 60).ToString("f2");
 
         e = float.Parse(seconds);
 
         timerText.text = seconds;
 
 
 
         if(finished){
 
             if(e < bestTime){
                 bestTime = e;
                 bestTime2.text = "Best: " + e;
             }
         
 
         }
 
 
 
 
     }
     }
     public void Finish()
     {
         finished = true;
     }
     public void Begin()
     {
         Begin2 = true;
         finished = false;
         startTime = Time.time;
     }
 }
 
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

Answer by Jaxcap · Dec 22, 2020 at 08:59 AM

I assume that you want the best time to be updated if finished = true? If the structure of your code is what I think it is (a little hard to tell due to formatting), I think this code:

              if(e < bestTime){
                  bestTime = e;
                  bestTime2.text = "Best: " + e;
              }

might not get used ever because you already have that other if(finished) block above that will just return before you can get there.

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
0

Answer by lvskiprof · Dec 22, 2020 at 09:36 PM

Since you have made it public, are you sure that some other code is not modifying it?

You should avoid using public data as much as possible.

If you use [SerializeField] you can view it in the Inspector without making it public while you run the program. I assume you have linked it in the Unity Editor to the text element in your game.

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

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

Related Questions

Add up updating float 2 Answers

Multiple Rich Text Not Working 1 Answer

Selecting a part of string in input field 1 Answer

How to connect a variable to a UI text object 0 Answers

show sync var on two text ui (unet - networking) 3 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