Search Issue Tracker
By Design
By Design in 6000.3.X
Votes
1
Found in
2021.3.43f1
2022.3.45f1
6000.0.18f1
6000.1.0a7
6000.2.0a1
6000.3.0a1
Issue ID
UUM-79277
Regression
No
SerializedObject.ApplyModifiedProperties doesn't mark Scene as dirty when called from OnValidate
Reproduction steps:
1. Open the attached “BugRepro” project
2. Open the “Assets/Empty.unity” Scene
3. Open the “Assets/ModifyAuto.unity” Scene
4. Observe the Console window
5. Re-open the “Assets/ModifyAuto.unity” Scene
6. Observe the Console window
Expected result: Nothing gets printed
Actual result: “Applying modified properties!” message is printed
Reproducible with: 2021.3.43f1, 2022.3.45f1, 6000.0.18f1
Reproducible on: Windows 11
Not reproducible on: No other environment tested
Notes:
- If the scene is open and you force the editor to recompile, the changes are recognized and saved as expected
- When the same code is executed via the context menu, the data is correctly saved
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- After converting a Built-in project to URP render texture related errors are spammed that can lead to Game view being rendered on top of Scene view
- UI Builder slider value lags and stutters when sliding/modifying certain property values
- "Reset UI Builder Layout" functionality inconsistently changes Canva Size when "Match Game View" is enabled/disabled
- Texture Import Warnings are obscured by other Terrain Layer options in the Inspector
- Burst Inspector middle divider is jittering when resized with the Burst Inspector window docked
Resolution Note:
Thank you for reporting a bug to Unity.
This behavior is actually as designed.
The 'OnValidate' method should primarily be used to validate the data and not to edit it.
Doing the edit at this point will not be taken into account and the dirty flag will not be set on the scene because it is still in the loading process.
If you want to edit a scene from a script on a GameObject in your scene you need to add the [ExecuteInEditMode] attribute on your MonoBehaviour class and register yourself to the EditorSceneManager.sceneOpened event.
Here is an updated example of your script to do so.
Please note that due to the nature of Events, you might need to do a domain reload to prevent accessing an old instance of the 'Test' component.
[ExecuteInEditMode]
public class Test : MonoBehaviour
{
[SerializeField] private List<BoxCollider2D> boxColliders;
void Awake()
{
EditorSceneManager.sceneOpened += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, OpenSceneMode mode)
{
EditorSceneManager.sceneOpened -= OnSceneLoaded;
TryToModify();
}
[ContextMenu("Modify")]
public void TryToModify()
{
// Same code here
}
}
Today we will be closing this case as "As Designed". Thank you again for taking the time to report this issue, and please let us know if there is anything else that changes the context or severity of this issue.
Resolution Note (6000.3.X):
Thank you for reporting a bug to Unity.
This behavior is actually as designed.
The 'OnValidate' method should primarily be used to validate the data and not to edit it.
Doing the edit at this point will not be taken into account and the dirty flag will not be set on the scene because it is still in the loading process.
If you want to edit a scene from a script on a GameObject in your scene you need to add the [ExecuteInEditMode] attribute on your MonoBehaviour class and register yourself to the EditorSceneManager.sceneOpened event.
Here is an updated example of your script to do so.
Please note that due to the nature of Events, you might need to do a domain reload to prevent accessing an old instance of the 'Test' component.
[ExecuteInEditMode]
public class Test : MonoBehaviour
{
[SerializeField] private List<BoxCollider2D> boxColliders;
void Awake()
{
EditorSceneManager.sceneOpened += OnSceneLoaded;
}
void OnSceneLoaded(Scene scene, OpenSceneMode mode)
{
EditorSceneManager.sceneOpened -= OnSceneLoaded;
TryToModify();
}
[ContextMenu("Modify")]
public void TryToModify()
{
// Same code here
}
}
Today we will be closing this case as "As Designed". Thank you again for taking the time to report this issue, and please let us know if there is anything else that changes the context or severity of this issue.