Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Editor Scripting - Undo.PerformUndo NullReferenceException

Discussion in 'Immediate Mode GUI (IMGUI)' started by The MC, Jul 7, 2012.

  1. The MC

    The MC

    Oct 31, 2005
    Hey Guys,

    So I'm experimenting with making my own custom editor tools, and I'm still kind of green on it. I have a tool that automates a bunch of actions on a GameObject for me (changing parent, transformation, rotation, etc). Anyways, I have it set up such that if I hit "Escape" before committing the actions, I would like the scene to perform and Undo and put my selection back the way it was.

    Everything "works" per se, however, whenever I hit Escape, I get a NullReferenceException from UnityEditor.SceneView.OnGUI (). The undo still occurs, I just get this error. What's also strange is that if I commit the actions, THEN do a normal "User" undo, everything works fine and I don't get the error.

    I make no assumptions that I'm not doing something TERRIBLY stupid, so any and all assistance would be greatly appreciated. Here's my code snippet:
    Code (csharp):
    2. public static void OnSceneGUI(SceneView sceneview)
    3.     {
    4.         Event currentEvent = Event.current;
    6.         // if User hits "escape"
    7.         if ( currentEvent.type == EventType.keyDown  currentEvent.keyCode == KeyCode.Escape )
    8.         {
    9.             // if the user is using a tool, clean up and Undo
    10.             if ( window.currentTool > -1  Selection.activeTransform )
    11.             {
    12.                 currentEvent.Use();
    13.                 SelectionCleanup();
    14.                 RecallTool();
    15.                 ResetToolbar();
    16.                 Undo.PerformUndo();
    17.             }
    18.             // otherwise, just clear the user's selection
    19.             else if ( Selection.activeTransform )
    20.                 Selection.activeGameObject = null;
    21.         }
    22. .......
    Here's the error I get:
    Code (csharp):
    2. NullReferenceException
    3. UnityEditor.SceneView.OnGUI () (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/SceneView/SceneView.cs:727)
    4. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
    And here's the Editor Log:
    Code (csharp):
    2. Unloading 410 unused Assets to reduce memory usage. Loaded Objects now: 981. Operation took 57.267323 ms.
    3. System memory in use: 14.9 MB.
    4. NullReferenceException
    5.   at (wrapper managed-to-native) UnityEditor.Handles:SetCameraFilterMode (UnityEngine.Camera,UnityEditor.Handles/FilterMode)
    7.   at UnityEditor.SceneView.OnGUI () [0x007b7] in C:\BuildAgent\work\14194e8ce88cdf47\Editor\Mono\SceneView\SceneView.cs:727
    9.   at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception)
    11.   at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
    13. (Filename: C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/SceneView/SceneView.cs Line: 727)
    If anyone has any ideas as to what silly mistake I made this time, I'd be grateful for a point in the right direction. Thanks guys!
  2. xWebber


    Jul 30, 2012
    Same here with OnGUI(). Everything works, but there's this exception..
  3. Arkade


    Oct 11, 2012
    Hey, sorry to necro this thread but since I found it while experiencing the same problem and then found an answer, I thought I'd leave it here to help the next traveller!

    The answer:

    The problem seems to be that we've issued an action in the Scene View but then change things such that it ends up redrawing and having a problem. (What's often called a reentrancy problem I believe.) So defer the code that's changing things to later and it avoids the problem. I was literally writing a level building tool too and hit the same problem -- mine was moving mouse out of the scene and doing Undo. It had all worked without undo but once I added that, I think the Undo is requesting a redraw and thus hitting the lack of reentrant safety. Just my guess.

    HTH :)