Permanent Variable Change in Editor

If you have a public variable you can change it in the Editor and the change will get saved, maybe that is what all the Serialization stuff is about. It works fine when you change it by hand but what if I want a script to run in the Editor to change the value?

From what I can tell, the change will get overriden. I attempted to have a second script that changes the value of the first one and it itself had the ExecuteInEditMode attribute. Unfortunately the value would be reset the next time I opened Unity. My second attempt was to create a custom inspector for the variable that needs to be set in the editor. I set the value in OnInspectorGUI on that one. Here are some snippets to show you what I mean:

public class Pickup : MonoBehaviour {
    public string id = "";
    ...
}

[CustomEditor(typeof(Pickup))]
public class Identifier : Editor {
    public override void OnInspectorGUI() {
        base.OnInspectorGUI();
        Pickup pickup = (Pickup)target;
        if (pickup.id == "") {
            pickup.id = Time.realtimeSinceStartup.ToString();
        } 
      ...
    }
}

The Identifier will set the pickup id but the pickup won’t remember after closing Unity. It works fine when I set it manually. What can I do to get it to save the value after the editor script changed it?

Alternatively, the bigger goal here is to have a unique identifier for each pickup, whenever I drag a pickup prefab into the scene. This identifier has to be permanent. If you happen to know a different solution for that problem, I will take that as well :wink:

Thanks
Jona

If goal is persisting data outside of playmode I’d recommend using scriptable objects for this, it will likely resolve your problem. Changes made to scriptable objects with playmode enabled will persist beyond :slight_smile:

I have seen an implementation where nearly every primitive value in the project was a reference to a scriptable object. For example, a players health would be a scriptable object of type ‘IntegerReference’. Then whenever you change that player health object, whether by editor script or in the inspector, that value will remain the same. In one of my projects I had a ‘PlayerStats’ scriptable object that could hold other scriptable objects for stats, inventory, etc, etc.

You can get started here: Unity Connect

Learning about scriptable objects was very interesting but then I found that the following script (Identifier.cs) worked too.

using UnityEngine;

[ExecuteInEditMode]
public class Identifier : MonoBehaviour {

    public string id = "";

    private void OnEnable() {
        if(id == "") {
            id = (Time.realtimeSinceStartup + GetInstanceID()).ToString();
        }
    }
}

Not sure what I did wrong before but it generates a fairly unique identifier that will stay forever. In my Pickup script, I use [RequireComponent(typeof(Identifier))] so it automatically gets an identifier.

https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data

This will show you how to persist data even when closing/opening Unity.