Search Unity

Editor script suddenly stops working

Discussion in 'Immediate Mode GUI (IMGUI)' started by Mish, Jan 26, 2015.

  1. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    I have made a custom editor script which can create objects when I press a certain key in the editor. This works well but sometimes the script simply stops working.

    I am assigning the method I am using in the OnEnable method of the script:

    public void OnEnable(){

    SceneView.onSceneGUIDelegate = CreateObject;

    }

    But this method suddenly stops getting called. I have tried the script on another machine in the same project and everything works fine. What could have caused this issue?
     
  2. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    You should never ever overwrite a global event handler like SceneView.onSceneGUIDelegate except you know what you are doing. You should only add yourself to the list of event handlers, using
    Code (csharp):
    1. SceneView.onSceneGUIDelegate += CreateObject;
    (the += instead of = is important ;)). Then, you should deregister yourself in OnDisable using -=

    Code (csharp):
    1.  
    2. void OnEnable() { SceneView.onSceneGUIDelegate += CreateObject; }
    3. void OnDisable() { SceneView.onSceneGUIDelegate -= CreateObject; }
    4.  
    What may happen for you is, that you got some other editor script also assigning to the delegate (and not using the proper +=). And this script overwriting your assignment.

    Unfortunately, "SceneView.onSceneGUIDelegate" is not marked as a C# "event" by unity or else you would get a compile error.
     
  3. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    Thanks for the answer, I once tried to add the event with += but for some reason the callback didnt work. However it seems the whole OnEnable method is not getting called.

    I also to tried to search the solution for another SceneView.onSceneGUIDelegate assignment, but there are no others.
     
  4. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    You DO create an instance of your EditorWindow somewhere, right?
    Just writing an EditorWindow class doesn't make it getting enabled.

    If you just want to have some code that gets executed without any visible window or menu item or whatever, you can use a static type initializer together with [InitializeOnLoad]

    Code (csharp):
    1.  
    2. [InitializeOnLoad]
    3. class MyClass
    4. {
    5.     static MyClass() { SceneView.onSceneGUIDelegate += CreateObject; }
    6. }
    7.  
     
  5. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96