What causes the NullReferenceException in Editor Window code
I have editor window where after loading some prefabs I put created game object out of prefabs into scene. For placing I use button where I register event dusringSceneGui with my function. Everything works just fine, but console gives me an NullReferenceException error after placing the object:
NullReferenceException
UnityEditor.SceneView.DoOnGUI () (at <9de32d58e117474d9df95577a7ba5c6d>:0)
UnityEditor.SceneView.OnSceneGUI () (at <9de32d58e117474d9df95577a7ba5c6d>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <d251562a4ccd481bac59212472bdabeb>:0)
This error is much longer but I find it useless to put here the whole message. Unfortunately there is no back tracing available, so I am not able to tell what causes this error. I suspect the SceneView.duringSceneGui might be culrpit, since error is SceneViewDoOnGUI(). Code snippet of main functions:
private void OnGUI()
{
// some nasty gui here
if (GUILayout.Button("Button"))
{
SceneView.duringSceneGui += PlaceObject;
}
}
void PlaceObject(SceneView sceneview)
{
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
Event e = Event.current;
Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);
RaycastHit hit;
if (e.type == EventType.MouseDown && e.button == 0)
{
if (Physics.Raycast(ray, out hit, 200f))
{
// Placing wokring prefab here, until here everything works
SceneView.duringSceneGui -= PlaceObject;
}
}
}
Your answer

Follow this Question
Related Questions
Camera for a Third Person Shooter dosen't work 1 Answer
Get the index of the currently selected HandleUtility.FreeMoveHandle in Scene View 0 Answers
create second sceneview with different scene 0 Answers
Controller gives NullReferenceException and Teleporter is unresponsive when collided 1 Answer
Null Reference 1 Answer