- Home /
Mark gameobject field as changed from prefab
I have a gameobject in a scene which is an instance of a prefab. Using some custom editor code, I can copy the position of a "door" and paste it into another door object.
Copying the door position...
Pasting the door position...
When I paste the position, the code is just setting the "Exit Position" field of the prefab.
So back in the scene, the gameobject does not recognize that the field has changed. The scene does not recognize that something has changed either...
Because of this, I can't save the scene with the new changed values unless I go in and change them manually, so upon leaving the scene the values will revert. Is there any way I can indicate to the editor that the values have changed?
Answer by Tezemi · Sep 26, 2018 at 01:24 AM
Here's the modified code implementing a solution I found using the SerializedObject and SerializedProperty classes, which were just what I needed.
Passing in a component into the SerializedObject constructor, you can then use the object to call the FindProperty method and get the field you wish to change (and have the editor recognize the change.) Then using the SerializedProperty instance, set the corresponding datatype field in the SerializedProperty to the value you want and then call ApplyModifiedProperties() on it. I know it's bit wacky, but it works great for what I needed to do. Now after using the "Paste Door Position" the values will be bold, the scene will be saveable, and the values will persist.
Answer by ShadyProductions · Sep 24, 2018 at 05:34 PM
Maybe you can use this call: https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager.SaveScene.html