Follow

How do I make a scene dirty when modifying a property via script?

Symptoms

  • I modify a property via the scripting API.
  • The scene is not made dirty.

Cause

Scripting APIs do not use the undo mechanism nor mark the scene dirty by default. One can, however, enforce these behaviours.

Resolution

There are two different options for solving this:

Option 1:

If you have access to the object itself you can use the Undo.RecordObject method before setting the property from the script.

Undo.RecordObject (myGameObject.transform, "Zero Transform Position");

myGameObject.transform.position = Vector3.zero;

An example showing using how the scene is marked dirty with Undo.RecordObject is attached to this article.

UndoRecordObject.png

Option 2:

One can also mark the scene as dirty manually using:

EditorSceneManager.MarkSceneDirty

after setting a property from script.

LightmapEditorSettings.bakeResolution = 14;

EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());

More information

Undo

EditorSceneManager

 




Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.