Changing GUI Text to int and applying operands
The basic issue is changing Gui Text to an int. What I'm trying to do is have an image clicked and have it pass an associated point value to the team score. Farthest I got was adding the score value to the team but ends up adding as a string (ex: Like instead of 25 + 50 = 75, I'll get 25 + 50 = 2550)
public class GiveValue : MonoBehaviour {
public int score; //THIS IS THE SCORE VALUE OF THE BUTTON
public GameObject TeamName; //THIS IS THE TEAM SCORE THAT SHOULD BE APPENDED
private int NewScoreText;
void Awake()
{
if (TeamName != null)
{
var ScoreText = TeamName.GetComponent<Text>();
//ScoreText.text = ScoreText.ToString();
//ScoreText.text = Int32.Parse(ScoreText.text) + score;
//int.Parse(ScoreText);
//timeLabel.Text = Int32.Parse(timeLabel.Text) - 1;
// ScoreText = ScoreText + score;
ScoreText.text = ScoreText.text + score;
Debug.Log("score is" + score); //HAS RETURNED A VALUE OF 50 PER WHAT I ENTERED
Debug.Log("ScoreText is" + ScoreText); //HAS RETURNED "TeamAScore (UnityEngine.UI.Text)"
Debug.Log("TeamName is" + TeamName); //HAS RETURNED "TeamAScore (UnityEngine.GameObject)"
}
}
}
What isn't commented out above works but does as I said, just adds as a string. The various commented out lines throw a collection of errors usually having issue of Text to int, string to int, or int to string. I'd like the Debugs to throw clean values, wasn't getting many clues from those.
Thanks for any suggestions, this one frustrates me...
Answer by DoodleDangWang · Mar 10, 2016 at 02:48 AM
Solved it:
int.TryParse(ScoreText.text, out NewScoreText);
NewTotal = NewScoreText + score;
ScoreText.text = NewTotal.ToString();
Just required a lot of pacing, screaming, n booze....