The data in the prefab is reset when exiting Unity

I store the data in a script that is attached to the prefab, everything works fine between the scene on and off sessions, but as soon as I exit the Unity and go back, the value resets to the one set in the inspector…

Here is the script used on the prefab:
9008401--1241611--DWVWEFWEF.png

And here’s what the prefab itself looks like.

If I change the values through the inspector, they are saved, but if I change them through the script, they keep their values only until I exit the Unity. Please help solve this

If your script is changing the instance of a prefab during runtime (Play mode), that’s intended behavior. To really change a prefab, you need to make your script work in edit mode, through AssetDatabase or a similar API.

All your prefabs and assets are basically protected during the Play mode, prefabs doubly so, because they are only ever instantiated into the scene, so you’re already modifying the copy.

edit:
If you’re talking about the editor script, you need to mark your asset as dirty, and make sure that the internal serialization state acknowledges the modification, but someone else will have to fill you in on the details.

2 Likes

Adding to the above, if you’re using a prefab asset as a form of data container… don’t do that; that’s not their intended usage. Use scriptable objects instead.

1 Like

I found this solution to my problem, thanks

using UnityEditor;

    public void SaveProgress()
    {
        EditorUtility.SetDirty(prefabScript);
    }
    private void OnApplicationQuit()
    {
        SaveProgress();
    }