- Home /
how to convert int to string in C#?
//1st script
string score="";
TimerScript.Seconds = score;
score = GUILayout.TextField(score);
GUILayout.EndHorizontal();
if(GUILayout.Button("Save Your Time Record!"))
{
HighScoreManager._instance.SaveHighScore(name,System.Int32.Parse(score));
highscore = HighScoreManager._instance.GetHighScore();
Application.LoadLevel("Main_Menu");
}
//2nd script
public float Seconds = 0;
i have an error like this error CS0029: Cannot implicitly convert type string' to
float' Corretion question ( How to convert string to float) please help me about this thankyou in advance! :/
You can search your error code to get a little tutorial on how to solve the error
I believe its a simple command like highscore = convertToString(score)
don't forget to mark the question as answered if it has been solved.
Answer by zombience · Dec 03, 2013 at 07:54 PM
float myNumber = 0.8f;
void Start()
{
Debug.Log(myNumber.ToString());
}
works for me
sir i have an error here score = TimerScript.Seconds; //error CS0122: `Timer.Seconds' is inaccessible due to its protection level :(
score = GUILayout.TextField(score);
GUILayout.EndHorizontal();
if(GUILayout.Button("Save Your Time Record!"))
{
HighScore$$anonymous$$anager._instance.SaveHighScore(name,System.Int32.Parse(score));
highscore = HighScore$$anonymous$$anager._instance.GetHighScore();
Application.LoadLevel("$$anonymous$$ain_$$anonymous$$enu");
}
if you have access to the TimerScript, either change the Seconds variable from private/protected to public, or add the keyword "public" in front of it, like so:
public class TimerScript
{
public float Seconds = .5f; // should now be accessible
}
it converts anywhere you call the function.
public class StringTest : $$anonymous$$onobehaviour
{
public string checkInspectorValue;
private float convert$$anonymous$$e = 0f;
void Update()
{
convert$$anonymous$$e += .01f;
checkInspectorValue = convert$$anonymous$$e.ToString();
}
}
if you put that script on a gameobject, check the "checkInspectorValue" output in the inspector. you'll see the float value, but it will be displayed as a string. this will occur anywhere you call the funcion .ToString() on a float, int, or other object that supports the conversion.
Answer by Owen-Reynolds · Dec 04, 2013 at 12:21 AM
The most common way is to add to any string, and let C# convert automatically:
""+score
ex: myGuiText.text = ""+score;
Your answer
