- Home /
How to Set the Game Speed Based on the Player's Current Score?
I want to make a change to my game. I am currently using the Player's saved High Score to dictate the speed of a rotating object in game. I have no problem grabbing the variable from PlayerPrefs, and it works perfectly fine in the sense of incrementally increasing the rotation speed based off the score. Here's the code as is:
public PlayerPrefs highScore;
public GameObject empty;
public float rotationSpeed;
void Start()
{
rotationSpeed = 8f;
}
void Update()
{
if (9 < PlayerPrefs.GetInt("highScore", 0))
{
rotationSpeed = 8f;
}
if (19 < PlayerPrefs.GetInt("highScore", 0))
{
rotationSpeed = 8.5f;
}
My question is, how do I use the Player's Current Score instead of their High Score to dictate this speed? I tried if (9 < currentScore.text) and other variations of that, but I can't seem to figure out how to grab the Current Score. I even referenced the Text object and it still wasn't right. This is all happening in the same scene, so I don't know why Unity is giving me trouble grabbing the variable and using it in the way I want to. Also, no questions on this forum related to this issue have provided me with any help. Any help on this would be much appreciated, thanks!
Text objects text property returns String[1]. The problem is if( 9 < currentScore.text) is wrong sytax. You cant compare int with String. Can you try to covert String to int first? -[1] https://docs.unity3d.com/ScriptReference/UI.Text-text.html
just like @Shameness say. you must convert currentScore.text to int first. try use int.Tryparse
// declare a string variable with a value that represents a valid integer
string silly$$anonymous$$eme = "9001";
int memeValue;
// attempt to parse the value using the TryParse functionality of the integer type
int.TryParse(silly$$anonymous$$eme, out memeValue);
Debug.Log(memeValue);//9001
As mentioned above, you can't compare string and int and should convert currentScore.text into int first. But how are you incrementing the current score? Wouldn't that be a separate int value that you might be using? If yes, you should use that variable in the if statement.
Answer by Adarsh2303 · Jun 30, 2018 at 09:09 AM
I Need little bit of more detail Related to it...like where are you storing current score and highScore... but before that try this...
void Update() { if (9 < PlayerPrefs.GetInt("highScore")) { rotationSpeed = 8f;
} if (19 < PlayerPrefs.GetInt("highScore")) { rotationSpeed = 8.5f; } }