Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question String as a math expression

Discussion in 'Scripting' started by Kasia93, Jul 7, 2022.

  1. Kasia93

    Kasia93

    Joined:
    Mar 15, 2022
    Posts:
    2
    Hi everyone!
    I have a problem that I struggle with for several days. I want to add two strings together (as a math expression).
    I have one script that works ok, but not as a math expression.
    I wrote several other scripts. I tried to convert string to float, and float to string to show up in UI Text Object but it's not working.
    I will be grateful for your help

    Code (CSharp):
    1. public GameObject timer1;
    2. public GameObject timer2;
    3.  
    4. public TMP_Text furdi;
    5.  
    6. public string thename;
    7.  
    8.     public void nana()
    9.     {
    10.         thename = timer1.GetComponent<TMP_Text>().text + timer1.GetComponent<TMP_Text>().text ;
    11.  
    12.  
    13. furdi.GetComponent<TMP_Text>().text = thename;
    14.  
    15. }
    16.  

    and this one isn't working
    Code (CSharp):
    1. public GameObject timer1;
    2.     public GameObject timer2;
    3.  
    4.     public TMP_Text thisss;
    5.  
    6.  
    7.  
    8.     float tonn;
    9.     float tonn2;
    10.  
    11.  
    12.  
    13.     public string thename;
    14.     public string thename2;
    15.  
    16.     public string end;
    17.  
    18.  
    19.     float essa;
    20.  
    21.  
    22.     public void nana()
    23.     {
    24.         thename = timer1.GetComponent<TMP_Text>().text;
    25.         tonn = float.Parse(thename);
    26.  
    27.  
    28.         thename2 = timer2.GetComponent<TMP_Text>().text;
    29.         tonn2 = float.Parse(thename2);
    30.  
    31.  
    32.         essa = tonn + tonn2;
    33.  
    34.         end = essa.ToString("00:00");
    35.  
    36.  
    37.         thisss.GetComponent<TMP_Text>().text = end;
    38. }
     
  2. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    266
    It would be very helpful if you state what exactly is not working, for example providing input and output values and state what you have been expected as output. One suspicious thing I can see in your code is the ToString-Format "00:00". If you want to specify the number of decimal places with 2 then you should use "0.00" as format string. Another problematic thing might be the float.Parse call. There may be conversion problems regarding to point and comma depending on your number settings in your OS.
     
    Bunny83 likes this.
  3. Kasia93

    Kasia93

    Joined:
    Mar 15, 2022
    Posts:
    2
    ohhh... I see my mistake!
    It was 00:00 becouse its Timer. Thank you very much (but you are right Im from Germany so OS trouble was as well involved). I have a new script. It`s working! But i still have one problem.
    I want that string.Format shows the minutes and seconds (00:00). But it gives seconds and nothing else.

    20 seconds look like this: 20:00
    and 2 minutes looks like this: 00:00

    Could somone maybe help me :)?

    Code (CSharp):
    1.         thename = timer1.GetComponent<TMP_Text>().text;
    2.         TimeSpan time = TimeSpan.Parse(thename);
    3.  
    4.         thename2 = timer2.GetComponent<TMP_Text>().text;
    5.         TimeSpan time2 = TimeSpan.Parse(thename2);
    6.  
    7.         TimeSpan time3 = time + time2;
    8.  
    9.         thisss.text = string.Format("{0:00}:{1:00}", time3.Minutes, time3.Seconds);
    10.  
     
    Last edited: Jul 8, 2022
  4. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    266