- Home /
EditorApplication.OpenScene causing NullReferenceException
I'm creating an editor window that includes functions for opening scenes. It's working and the scenes open, but I'm getting null reference errors that I can't get rid of:
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.DockArea.EndOffsetArea ()
UnityEditor.DockArea.OnGUI ()
Anyone know how to fix this?
Below is a script I've tested in a new project. The test scenes have nothing in them except a cube and a sphere.
class TestWindow extends EditorWindow {
@MenuItem("Window/TestWindow")
static function Init() {
var window : TestWindow = EditorWindow.GetWindow(TestWindow);
}
function OnGUI() {
var openTest1 : boolean = false;
EditorGUILayout.BeginHorizontal();
if(GUILayout.Button("Test1", EditorStyles.toolbarButton, GUILayout.Width(100))) {
openTest1 = true;
//EditorApplication.SaveCurrentSceneIfUserWantsTo();
//EditorApplication.OpenScene("Assets/Test1.unity");
//return;
}
if(GUILayout.Button("Test2", EditorStyles.toolbarButton, GUILayout.Width(100))) {
EditorApplication.SaveCurrentSceneIfUserWantsTo();
EditorApplication.OpenScene("Assets/Test2.unity");
return;
}
EditorGUILayout.EndHorizontal();
if(openTest1) {
EditorApplication.SaveCurrentSceneIfUserWantsTo();
EditorApplication.OpenScene("Assets/Test1.unity");
}
}
}
you should never have a return statement in between the starthorizontal and endhorizontal, unless the endhorizontal is in a finally block wrapping the return statement, try this:
EditorGUILayout.BeginHorizontal();
try {
// your code here.
} catch (Exception e) {
Debug.LogError(e.$$anonymous$$essage+"\n"+e.StackTrace);
} finally {
EditorGUILayout.EndHorizontal();
}
The advantage here is if you have an error, the gui does not fail too.
Your code for the Test1 button looks like it should work to me though. So I'm not sure.
Answer by Steven-Walker · Oct 18, 2011 at 08:56 PM
EditorGUIUtility.ExitGUI();
Cheers! This really should be demonstrated and explained in the button documentation.
Your answer
![](https://koobas.hobune.stream/wayback/20220612222931im_/https://answers.unity.com/themes/thub/images/avi.jpg)