- Home /
Level PlayerPrefs wont increase after a game
public class LevelPrefs : MonoBehaviour { public Transform playerPrefRef; public Text textPrefRef; int lvlTextInt = 1; bool incrementBool = true; int i;
private void Start()
{
incrementBool = true;
if (playerPrefRef.position.z <= -86 && incrementBool)
{
PlayerPrefs.SetInt("Current Int", lvlTextInt += 1);
incrementBool = false;
//lvlTextInt++;
}
}
private void Update()
{
textPrefRef.text = PlayerPrefs.GetInt("Current Int", lvlTextInt).ToString();
}
void playerPrefsMeth()
{
PlayerPrefs.SetInt("Current Int", lvlTextInt);
}
public void resetPrefs()
{
PlayerPrefs.DeleteKey("Current Int");
}
} it at first the level of the game is 1 then after it reaches the end the int becomes 2 but afterwards it doesn't increment at all if you play the game again or keep playing because after you reach the finish line it resets and it starts with 2 now but when it finishes it doesn't becomes 3 or stack up.
Answer by jackmw94 · Nov 08, 2020 at 06:41 PM
It's hard to debug without a bit more context but a few clues here are:
As far as I can see, your playerPrefsMeth function never gets called meaning you only ever set that player pref in Start.
Also your Start function is the only thing that increments the levelTextInt and since it always starts at 1, this value will only ever be either 1 or 2. Even if you create new ones, these new ones will initialise the levelTextInt to 1.
I expect you want a function that you call when you complete a level that will be used to increment your levelTextInt:
public void LevelCompleted()
{
levelTextInt++;
playerPrefsMeth(); // (to update the player prefs)
}
If you don't have one already, use an IDE that attaches to Unity so you can use breakpoints, then you can see when/if you hit various parts of your code and what the values being used are.
so I'm going to have a start method that checks an if statement and after sometime the game has been played and the conditions have been met the code in the if statement will execute, right? which will consist of making the bool false and calling the method that you gave. in the method it increments the integer and is it just suppose to call the playerPrefs$$anonymous$$eth? without any parameter of anything or any changes here and there? Sorry I'm really new to this
We've all been new at some point!
Because all this code is synchronous, it'll all happen at the moment that it's called. So if you want something to happen after some time once certain conditions have been met, that code should not be in Start since everything in Start will only happen at the Start of this component (i.e. when it first gets loaded when the scene starts OR if you generate it, then when it gets created). Instead, put that code in Update so it can be checked every frame - then it will run as soon as all conditions are met.
If you can give me a little more information about what else calls this script then I think I'll be able to help a bit better: Is it playerPrefRef.position.z <= -86
that deter$$anonymous$$es whether the level is complete? Does your game always start at level 1? Or are you using player prefs to start where they left off? What are you trying to do with 'incrementBool'?
Thanks for spending time on this. this script is attached on the game manager and in the script I reference an object cause it moves on the z axis and if that object reaches a certain point (playerPrefRef.position.z <= -86) then the game has finished and I want to just increment an integer in the playerprefs but idk it just wont and here is the deal there is only one level scene when the level if finished the same scene is loaded meaning the level restarts which is exactly what i need and that integer increasing after every level passed. i tried the update method too instead of start but tend to increment the int every frame so in implemented the increment bool variable so at default it is true meaning it has permission to increment so it only checks for playerPrefRef.position.z <= -86 and when the conditions are met increment player pref integer and make increment bool variable false so it only gets called once in the update method. hope this helps.
Your answer
Follow this Question
Related Questions
Load scene transistions 1 Answer
I am making a level editor and need ideas on how to save the level file. 1 Answer
How can I lock a Level? 2 Answers
How to generate levels in unity from ogmo editor? 0 Answers
Problems with unlocking levels 1 Answer