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
public GameObject timer1;
public GameObject timer2;
public TMP_Text furdi;
public string thename;
public void nana()
{
thename = timer1.GetComponent<TMP_Text>().text + timer1.GetComponent<TMP_Text>().text ;
furdi.GetComponent<TMP_Text>().text = thename;
}
and this one isn’t working
public GameObject timer1;
public GameObject timer2;
public TMP_Text thisss;
float tonn;
float tonn2;
public string thename;
public string thename2;
public string end;
float essa;
public void nana()
{
thename = timer1.GetComponent<TMP_Text>().text;
tonn = float.Parse(thename);
thename2 = timer2.GetComponent<TMP_Text>().text;
tonn2 = float.Parse(thename2);
essa = tonn + tonn2;
end = essa.ToString("00:00");
thisss.GetComponent<TMP_Text>().text = end;
}
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.
1 Like
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 :)?
thename = timer1.GetComponent<TMP_Text>().text;
TimeSpan time = TimeSpan.Parse(thename);
thename2 = timer2.GetComponent<TMP_Text>().text;
TimeSpan time2 = TimeSpan.Parse(thename2);
TimeSpan time3 = time + time2;
thisss.text = string.Format("{0:00}:{1:00}", time3.Minutes, time3.Seconds);
Yep, that is an important information that you want to use timespan values and not floating point ones 
I advice looking at the microsoft docs for .NET (Standard 2.0 or 2.1, depending on your Unity version):
You have to use some characters instead of numbers for the format string. I am sure you find out yourself which format string to use.