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
Thanks
Jona