- Home /
Playerprefs.SetInt Problem
when scene change then update level text but problem was there every time 2 increase when scene change not 1 increase
Code:
public string currentscenename; //Store Level 1
public string nextscenename; //store Level2
void Start()
{
leveltext.text = "LEVEL " + PlayerPrefs.GetInt("Level", 0).ToString();
}
void FixedUpdate()
{
leveltext.text = "LEVEL " + PlayerPrefs.GetInt("Level", 0).ToString();
//some code here
}
public void Next()
{
int num = PlayerPrefs.GetInt("Level", 0);
num++;
PlayerPrefs.SetInt ("Level",num) //problem here every time increase 2 not 1 add
if (currentscenename == nextscenename)
{
Next();
}
else
{
SceneManager.LoadScene(nextscenename);
}
}
Answer by tormentoarmagedoom · May 22, 2019 at 08:20 AM
Hello.
First, where Next() is called for 1st time?
Second, You need to debug your code while runnig man.... Then will see why "its icreasing 2"...
OR you are running it twice, or you are increasing the value in any other place...
You must learn to debug your problem...
Thanks Reply,Next() is called in Switch Statement
public void Next()
{
int num = PlayerPrefs.GetInt("Level", 0);
Debug.Log("Num1:" + num);
num++;
Debug.Log("Num2:" + num++); //increase 1
PlayerPrefs.SetInt ("Level",num); //but this line increase 2
Debug.Log("Num3:" + num++);
if (currentscenename == nextscenename) //both are string
{
Next();
Debug.Log("InsideIf");
}
else
{
Scene$$anonymous$$anager.LoadScene(nextscenename);
Debug.Log("INSIDE ELSE");
}
}
problem is there PlayerPrefs.SetInt ("Level",num); when reach this line then increase 2??
Increasing the value of num inside a logging call is strange, to say the least. Any reason you're doing that? Are you aware that after these 2 lines you'll have incremented it twice?
num++;
Debug.Log("Num2:" + num++);
@Bonfire-Boy @tormentoarmagedoom hey i m using only one time num++??
Your answer
Follow this Question
Related Questions
Classic Resident Evil-style room loading/level streaming? 4 Answers
How do I load previous levels individually in the build settings in unity3d 0 Answers
Re-loading a scene but on the background older scenes are displayed 1 Answer
Is there any ideas to load Unity scene asynchronously? 1 Answer
Using parameters of activeSceneChanged? 0 Answers