-1

I have the below code. What could possibly make this not work? Other PlayerPrefs seem to work fine. The log always shows that it restores "" and yet it always saves my actual text.

EDIT: I've just discovered that my "OnDisable" code is being called before "Start". I didn't really think that was possible, but that's the problem. So I guess my question is changing a bit...

EDIT 2: OnDisable was being called before Start because another "Awake" function was disabling this object which apparently immediately runs OnDisable.

public InputField ModuleList;

void Start()
{
    ModuleList.text = PlayerPrefs.GetString("ModuleSet", "");
    Debug.Log("Restoring " + PlayerPrefs.GetString("ModuleSet", ""));
}

public void OnDisable()
{
    Debug.Log("Saving " + ModuleList.text);
    PlayerPrefs.SetString("ModuleSet", ModuleList.text);
}
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
  • BTW these days Save is pretty performant. In almost all cases you can just do it whenever you want. In the "old days" you would always see warnings like "only do a Save when you are not busy" These days that issue is almost always irrelevant. You can pretty much just Save everytime you do a SetString. Enjoy! – Fattie Mar 03 '20 at 15:22

1 Answers1

2

call

PlayerPrefs.Save();

for saving data

Make sure you do not disable you GO before Start() method being called

Nikolay Gonza
  • 603
  • 5
  • 18
  • Weird. I've never called a Save and it's always saved. When is this necessary and when is it not? – CodeMonkey Mar 03 '20 at 15:17
  • as said in this link: https://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html By default Unity writes preferences to disk during OnApplicationQuit() so you may be didnt quit app and try to read your prefs, to save it during session you need to Save() method – Nikolay Gonza Mar 03 '20 at 15:19
  • you need the Save for it actually to work "between the user playing the game different times". it will work "locally" ("during play") if you have not done a Save, which is what may have throw you off, @CodeMonkey Quite simply, you have to Save if you want to know the values the next time the user opens the app. – Fattie Mar 03 '20 at 15:21
  • Ah, interesting. In my case, I've just discovered the problem but still not the solution. My "OnDisable" is somehow being called before my "Start"! – CodeMonkey Mar 03 '20 at 15:22
  • due to execution order, it is impossible, can you give more info link: https://docs.unity3d.com/Manual/ExecutionOrder.html – Nikolay Gonza Mar 03 '20 at 15:28
  • That's what I thought too... So then I thought maybe it was because I indiscriminately deactivated it even though it was already inactive, but checking if it was active first didn't solve it. I must be doing something really dumb, but either way impossible is impossible, right? So what the heck? :-P – CodeMonkey Mar 03 '20 at 15:30
  • try move from start to OnEnable() method and check if it works – Nikolay Gonza Mar 03 '20 at 15:35
  • It does make a difference. Now it Enables, Disables, Enables, Starts, and my stuff is saved. A bit unsettling though... – CodeMonkey Mar 03 '20 at 15:43
  • the problem i think that you expect that every enabling Start method being called, but it calls only once when script is initializing – Nikolay Gonza Mar 03 '20 at 15:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208934/discussion-between-codemonkey-and-nikolay-gonza). – CodeMonkey Mar 03 '20 at 15:46
  • I was indeed accidentally disabling my object before Start could be run. Oops!! – CodeMonkey Mar 03 '20 at 16:28