Reputation: 47
I am trying to make a high score system for my game in unity and am not really sure on how to do that, but I tried this code nonetheless and I keep receiving error messages.
I have tried to search up these errors but since I am quite new to unity and coding in general none of the solutions there make sense to me.
public Transform player;
public Text scoreText;
public Text highScore;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString("0");
scoreText = scoreText.int.Parse;
if (scoreText > PlayerPrefs.GetInt("High Score", 0)) ;
{
PlayerPrefs.SetInt("High Score", scoreText);
highScore.text = scoreText.ToString;
}
These are the error messages that show up
Assets\Score.cs(15,31): error CS1001: Identifier expected
Assets\Score.cs(15,31): error CS1002: ; expected
Upvotes: 1
Views: 1723
Reputation: 1559
Try this code:
public Transform player;
public Text scoreText;
public Text highScore;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString("0");
int scoreTextNumber = int.Parse(scoreText.text);
if (scoreText > PlayerPrefs.GetInt("High Score", 0))
{
PlayerPrefs.SetInt("High Score", scoreTextNumber);
highScore.text = scoreTextNumber.ToString("0");
}
Upvotes: 0
Reputation: 1909
What you have presently won't work. It would be nice if you could learn the basics syntax of C# to possibly avoid problems like this.
Text
is a class and scoreText
in this case is an instance of the Text
class. You can't just do this:
scoreText = scoreText.int.Parse;
Unless of course, scoreText.int.Parse
(doesn't work though) returns a Text
.
I think what you're trying to do is to get the player's position and convert to int, so you could save the value using PlayerPrefs
.
You can simply create an int variable.
private int _score;
Then use like this:
_score=int.Parse(scoreText.text);
You can the save using PlayerPrefs
.
PlayerPrefs.SetInt("High Score", _score);
I hope this helps.
There is also an error on this line:
highScore.text = scoreText.ToString;
Should be :
highScore.text = scoreText.ToString();
Because ToString()
is a method.
Upvotes: 1
Reputation: 717
Remove the semicolon after the If condition.
void Update()
{
scoreText.text = player.position.z.ToString("0");
// scoreText = scoreText.int.Parse;
if (scoreText > PlayerPrefs.GetInt("High Score", 0))
{
PlayerPrefs.SetInt("High Score", scoreText);
highScore.text = scoreText.ToString();
}
let me know if it helps
Upvotes: 1