Prefab public field changes during playtime in editor remain after?


I have a script that holds an array of prefab objects. Each of these objects has another script on them with a public bool that is responsible for determining whether or not the object is unlocked. I’ve found that while in play mode (in the editor) if I unlock one or more of these objects (through the game, not the editor), that the changes remain after exiting play mode. So, for example, if I unlock an object during play mode, then it remains unlocked after exiting play mode.

It’s somewhat annoying having to go back through the editor and reset all the values of the prefabs after running every time, but worse is that it’s making it a royal pain in the but to debug. Anyway, I was wondering if anyone can shed some light on why these changes remain after exiting play mode. Many thanks in advance

Do use anything like Resources.FindObjectsOfTypeAll? That will find all instances in the game and in the project folder. Or do you directly reference your prefab from the project tab by dragging them into the inspector? You want to only be finding instances of the prefab in the scene.

Hey thanks for your response. I’m directly referencing the prefabs which seems to be the issue. I figured out a work around but it was quite the task figuring out exactly what was going on. Since the prefabs were being altered post play mode in the editor, I figured they would behave the same on the mobile platform I’m targeting but that appears to not be the case. Are there any particular reasons I wouldn’t want to alter the prefabs directly? (Other than the issue I ran into)

For anyone who reads this in the future, here’s an explanation of the issue and how I’ve temporarily handled it:

I had an array of GameObject prefabs in a simple script. Each prefab had a script on it that had a bool for determining whether or not the object was equipped. Only one item could be equipped at a time. When an item became equipped, I would save the equipped status of all items to an array of bools in a binary file via binary formatter (look up persistent memory if unfamiliar). The key here is that in the inspector, I defined the first prefab in the array as being unlocked.

This all worked fine in the inspector, but when I would run the game stand alone, poop went awry. I would run the game, select a new item to be equipped and then save. Then, I would run the game again, but both the first item and the item unlocked on the previous go around would show as equipped. The issue was that even though I was loading the saved data, the prefab was overriding the loaded data with its prefab values. Since the first item in my prefab list was set as equipped by default, it was equipping itself every time the scene would boot up.

The work around I discovered was to reiterate through the array of items, un-equip all of them, and then equip the proper one. It’s dirty, but if you’re in a pinch like me, it works.

Hope this helps someone at least understand the issue someday