- Home /
Like PlayerPrefs but clears when game starts
My game has a main menu scene and the options in the menu reuses scenes that have an initialization string to customize the scene.
if (GUILayout.Button (items[i].label)) {
PlayerPrefs.SetString("TaskCode", items[i].taskcode);
Application.LoadLevel(items[i].levelname);
}
If I load the scene manually rather than using the main menu scene it uses the last saved instance of "TaskCode" - even if it is meant to be used for a different scene because it stays saved between games.
Ideally I want "TaskCode" in PlayerPrefs to clear when the game first starts then if the other scene is loaded with no value for "TaskCode" it would use a default value set based on the scene.
Answer by RedDevil · Aug 12, 2014 at 12:30 PM
Use PlayerPrefs.DeleteAll(); in the start function of that script.
Hi I added Delete$$anonymous$$ey to the start of the main menu and to the end of the other scenes. That way the main menu passes the string to the other scenes and then the strings are deleted. So if another level is loaded directly it has no string saved.
Answer by Kiwasi · Aug 13, 2014 at 04:04 AM
An alternative strategy would be to use a static variable. Player prefs is not really needed if you don't want data saved between sessions. And there is a small overhead to using it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Member variable not persistent 0 Answers
Errors and suggestions 2 Answers
Best way to persist data while we are in a not final version 4 Answers
Is it safe to store & read data to XML file for mobile game development 1 Answer
Converting float to string with time format; sending variables to disabled game object 1 Answer