- Home /
Converting String to Integer is not working
Hi, so I have this script and I think it should work but when I run it, it says: FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629) Score.Update () (at Assets/Scripts/Score.cs:26)
The Script:
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text scoreText;
public Transform player;
public string PlayerScore;
public Text highScore;
//public int highScoreValue;
void Start()
{
highScore.text = PlayerPrefs.GetString("Highscore").ToString();
}
void Update () {
scoreText.text = player.position.z.ToString ("0");
PlayerScore = player.position.z.ToString ("0");
if ( System.Convert.ToInt32(PlayerScore) > System.Convert.ToInt32(PlayerPrefs.GetString("Highscore"))) {
PlayerPrefs.SetString ("Highscore", PlayerScore);
}
}
}
Is there a reason you are keeping score as a string and not a number?
Answer by ghostmode · Oct 13, 2017 at 07:36 AM
If you really want to use strings for this, then try getting the high score like this:
System.Convert.ToInt32(PlayerPrefs.GetString("Highscore", "0"))
That said, this could be made a lot simpler by just using floats instead of strings, let me know if you're interested and I'll show how.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140853im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help! Converting Android game to iOS platform. What to do about IAP? 0 Answers
How to convert a float to a string and then have it appear in a gui rect 3 Answers
Spawn Script problem 2 Answers
Getting the Nth digit of an interger. (Js) 1 Answer
Is there something like Animator.StringToHash() but for tags? 0 Answers