How do you change UI Text to an int?
I am trying to make a statistic menu in my game which will show you stats about your progress in the game. I am trying to make a text that will tell you how much time you have played the game.
#pragma strict
import UnityEngine.UI;
static var gamesPlayed : int;
var gamesPlayedText : UI.Text;
function Start () {
gamesPlayed = gamesPlayedText.ToInt();
gamesPlayedText = GetComponent(UI.Text);
gamesPlayedText.text = PlayerPrefs.GetInt("gamesPlayedText",0).ToString();
}
function Update ()
{
PlayerPrefs.SetInt ("gamesPlayedText", gamesPlayed);
gamesPlayedText.text = gamesPlayed.ToString();
}
The problem with this script is that I want it to set the gamesPlayed variable to the gamesPlayedText variable so it won't go back to 0 whenever I leave the game and go back in. I know that there is a ToString() but when I try ToInt() it gives me an error that it doesn't exist. So Is there a way to make a UI Text an integer? Please answer and Thanks in advance.
Answer by MaxGuernseyIII · Jan 13, 2018 at 07:55 PM
In C# it would be
int.Parse(gamesPlayedText.text);
Here's an answer provided to the last person who asked this question as pertains to UnityScript.
https://answers.unity.com/questions/37756/how-to-turn-a-string-to-an-int.html
I don't know why you are punishing yourself with UnityScript.
Answer by Mercbaker · Jan 08, 2018 at 12:22 AM
gamesPlayed = Integer.parseInt(gamesPlayedText.text);
I tried this code, but it gave me an error $$anonymous$$ indetifier: Integer
Answer by lspence812 · Jan 11, 2018 at 09:32 PM
Use int.parseInt(gamesPlayedText.text):
I am getting the error ‘parseInt’ is not a member of ‘int’.
Answer by KingKline · May 23, 2020 at 05:22 AM
intvariable = int.Parse(txtvariable.text);
Replace "intvariable " and "txtvariable"