Resize Editor Window - Forced Undock Error

I am resizing an editor window by directly changing the value of it’s position. This works fine, except when the EditorWindow is docked and it is resized beyond the maximum size allowed in the docked position. The EditorWindow will undock and resize correctly, but a Null Reference Exception is displayed:

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.DockArea.EndOffsetArea ()
UnityEditor.DockArea.OnGUI ()

I tried catching the exception with no luck. How can I avoid having to see this error when an EditorWindow is forced to undock?

You shouldn’t undock your window in OnGUI since this will break the GUI groups. You could do it in an update delegate or try to using the undocumented function GUIUtility.ExitGUI(); right after you set the position in OnGUI. This function will terminate the current OnGUI call to usually prevent such errors.

GUIUtility.ExitGUI(); has been mentioned in various posts, but it can of course be found in MonoDevelop / Visual Studio thanks to the code completion :wink: