Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020.2 has been released.
    Dismiss Notice
  3. 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

Sharing a variable between OnSceneGUI and OnInspectorGUI

Discussion in 'Scripting' started by Neillium, Jan 24, 2017.

  1. Neillium


    Sep 21, 2014
    Hello Unity Community,

    Recently, it seems that one of my custom inspector scripts has stopped working properly. I used to be able to access a member variable in both OnSceneGUI and OnInspectorGUI that would retain the same value, but this seems to no longer be the case. A minimal example is the following:

    Code (CSharp):
    1. [CustomEditor(typeof(TestScript))]
    2. public class TestScriptInspector : Editor
    3. {
    4.     private int testInt;
    6.     public void OnSceneGUI()
    7.     {
    8.         Debug.Log("In OnSceneGUI: " + testInt);
    9.     }
    11.     public override void OnInspectorGUI()
    12.     {
    13.         testInt = 1;
    14.         Debug.Log("In OnInspectorGUI: " + testInt);
    15.     }
    16. }
    This inspector now produces "In OnSceneGUI: 0" and "In OnInspectorGUI: 1" for me. Retaining values of member variables in an Editor class like this used to work for me; I'm not sure when it stopped (within the last couple of months perhaps). Any idea why? Any workarounds that you can think of? I need to have unique values for each object, so static variables won't work for me, for instance.

    There are a couple of questions related to this problem, but no solutions that work for me:

    Thanks in advance!
  2. Neillium


    Sep 21, 2014
    As a (hopefully) temporary fix, I've implemented a workaround where I replace the member variable (testInt in the example above) with a static Dictionary<int, int>, that is keyed by the instance ID of the object associated with the inspector, with a value that is the int I need to use in both OnSceneGUI and OnInspectorGUI. This allows for setting the value in one method and retrieving it in the other, while having a unique value for each object. A silly thing to have to do, but it works in a pinch, and I'm not too bothered by it since it's only editor code. Hopefully this helps someone.
  3. ThermalFusion


    May 1, 2011
    I just recently ran into this aswell. Logging the instance id of the Editor yielded two different ones, which got me thinking two different editors were running with the same target.
    Didn't have time to delve deeper into it. Is your object a prefab instance in the scene? And do you experience the same if it is not?
  4. Neillium


    Sep 21, 2014
    I just verified that the InstanceID of the editor is indeed different in OnSceneGUI and OnInspectorGUI. Fortunately, the InstanceID of the target is the same in both methods. In my case, the target object is not a prefab.
    ThermalFusion likes this.
  5. ramy_d


    Oct 12, 2014
    I have code from 5.5 that def. broke because of this. Did anyone file a bug?
  6. Deleted User

    Deleted User


    I know this is an old post, but maybe somebody runs into this problem.
    It seems that the people at Unity transfered the SceneView and the Editor in individual objects (instances), hence the different instance ID's.

    If you need to share values of variables between OnSceneGUI() and OnInspectorGui() just declare them as "static". Then those variables won't be instantiated for each instance of the SceneView or Editor.
    Romaleks360 and Damjan-Mozetic like this.