Search Unity

Resolved How do you set a TMP text to a float?

Discussion in 'Scripting' started by Noodle_Meanie, May 20, 2023.

  1. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110
    I have a float called waitTime, and a text. I need the text to be waitTime.
    Code (CSharp):
    1. if(-0.1f < waitTime)
    2.         {
    3.             waitTime -= Time.deltaTime;
    4.             text.text = "text"; // <--- this is where it needs to be set to waitTime
    5.         }
    6.         else
    7.         {
    8.             text.text = "";
    9.         }
    If you need to know anything else ask.
    Thanks.

    -----------------------------------------------------------
    Solution. Thanks to spiney199.

    I changed my code to this:
    Code (CSharp):
    1. if(-0.1f < waitTime)
    2.         {
    3.             waitTime -= Time.deltaTime;
    4.             text.text = (Mathf.Round(waitTime).ToString());
    5.         }
    6.         else
    7.         {
    8.             text.text = "";
    9.         }
     
    Last edited: May 20, 2023
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    Every C# object has a
    .ToString()
    method that does what it says on the box.
     
    Noodle_Meanie likes this.
  3. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110
    Thank you. I've tried something with .ToString() before so I tried to do something different and it worked.
     
  4. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    75