- Home /
Saving changes in a component modified by an editor script
I need to give a few objects an unique id. Instead of doing it by hand I wrote an editor script.
The script works perfectly, but it doesnt save the changes even after i save the scene (it reverts the id to 0 when i enter play mode)
Here's the script (the relevant part) :
public bool assignedID = false;
public string id = "";
void Awake()
{
if (Application.isPlaying) return;
if (!assignedID)
{
// these work perfectly, ignore them
assignedID = true;
id = GetUnusedID();
// none of this works
EditorUtility.SetDirty(this);
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene());
}
}
Really need to see the whole script to help properly. But, if your "id" field was part of the mono behaviour and not the editor script, it would serialize properly.
This is the full script: http://pastebin.com/fbZajVGd
I think your problem might be that Unity only serializes certain primitive variable types. Not sure if Long is one of them. Have you tried making it an int ins$$anonymous$$d to see if the serialisation works?
Answer by Hypnotoad0 · Mar 20, 2016 at 01:56 PM
After 20 years of trial and error I seem to have fixed it. You have to break the prefab instance or it will keep resetting.
PrefabUtility.DisconnectPrefabInstance(this);
There's probably a better workaround but this one worked for me.
Tried that. Nothing worked for me until i broke the prefab.
Answer by HalfBlood_Elf · Oct 25, 2018 at 11:19 AM
Had similar problem, changed fonts in editor but no saving. Helped this topic: https://answers.unity.com/questions/1042052/change-text-in-text-component-from-editor-script.html
Your answer
Follow this Question
Related Questions
Help with Missing Monobehaviours and Asset Serialization? 0 Answers
Got a Problem when serializing Class Dictionary with Custom Editor 1 Answer
[JS] [Editor] SerializedObject from a custom class. 0 Answers
Editor Serialization of Collections/Collection Interfaces 1 Answer
ScriptableObject : Serialized Polymorphism Classes Can Not be Deserialize with Polymorphism 1 Answer