- Home /
If statement bug? (Glitch)
For some reason my if statement is not working as it should.
private void Update()
{
i = Mathf.Round(transform.position.z + 14.96f);
tm.text = i.ToString() + " m";
}
public void HighScore()
{
GameObject HighScoreObj = Instantiate(highScore);
HighScoreObj.transform.position = new Vector3(transform.position.x + 4.4f,transform.position.y + 3.2f,transform.position.z - 0.12f);
if (i > PlayerPrefs.GetFloat("HighScore"))
{
highScore.GetComponent<TextMeshPro>().SetText("New Record!");
PlayerPrefs.SetFloat("HighScore", i);
PlayerPrefs.Save();
}
if (i < PlayerPrefs.GetFloat("HighScore"))
{
highScore.GetComponent<TextMeshPro>().SetText("Record " + PlayerPrefs.GetFloat("HighScore") + "m");
}
Time.timeScale = 0;
}
It is setting the text of what it should have been in the last playmode. For example, if i = 10
and PlayerPrefs.Float = 5
when HighScore() is called the text changes to "Record "
. And if I enter another playmode test, it will make the text say "New Record!"
even if i
is less then the playerpref float. So summed up, it pretty much returns the if statement
of the last playmode test. Is this a glitch with Unity or what? It is very frustrating because it is such basic code and yet I have not got it working, so any help would be very appreciated!
try
if (i > PlayerPrefs.GetFloat("HighScore"))
{
highScore.GetComponent<Text$$anonymous$$eshPro>().SetText("New Record!");
PlayerPrefs.SetFloat("HighScore", i);
PlayerPrefs.Save();
} else {
highScore.GetComponent<Text$$anonymous$$eshPro>().SetText("Record " + PlayerPrefs.GetFloat("HighScore") + "m");
}
Also check your playerprefs is saving properly. Get its value before entering your if statement.
That was actually my code to begin with, thanks for trying to help anyways. I think it is definitely a bug, I have parsed the float in my start function and it is definitely saving properly. I'll update this if I get it resolved!
Answer by Buretto · Dec 07, 2017 at 04:07 AM
OMG finally! It turns out it was an error with TextMeshPro
. I had to convert to UGUI and it finally works!
Your answer
Follow this Question
Related Questions
Point of light bug 0 Answers
[Error] Error While Importing Packages 0 Answers
Variable changes to a negative number somehow 1 Answer
Assets Blinking on Game Play 0 Answers