Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Editor script variables being set magically....

Discussion in 'Immediate Mode GUI (IMGUI)' started by Game-Whiz, Apr 13, 2014.

  1. Game-Whiz

    Game-Whiz

    Joined:
    Nov 10, 2011
    Posts:
    122
    Hi,

    I have this test editor script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(GUIStuff))]
    6. public class GUIStuffEditor : Editor {
    7.  
    8.     bool selection0=false, selection1=false;
    9.     int toolBarSelection=0;
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         string[] toolBarOptions = {"Selection 0", "Selection 1"};
    14.         toolBarSelection = GUILayout.Toolbar(toolBarSelection, toolBarOptions);
    15.  
    16.         Debug.Log ("Going to change bool vars");
    17.  
    18.         selection0 = (toolBarSelection == 0);
    19.         selection1 = (toolBarSelection == 1);
    20.  
    21.         Debug.Log ("OnInspectorGUI: Selection 0: "+selection0+ " Selection 1:"+selection1);
    22.     }
    23.  
    24.     public void OnSceneGUI()
    25.     {
    26.         Debug.Log ("OnSceneGUI: Selection 0: "+selection0+ " Selection 1:"+selection1);
    27.     }
    28.  
    29.  
    30.     [MenuItem ("GameObject/Create Other/GUI Stuff")]
    31.     static void createGUIStuffObject() {
    32.        
    33.         GameObject newObj = new GameObject("GUI Stuff");
    34.         GUIStuff stuff = newObj.AddComponent<GUIStuff>();      
    35.     }
    36. }
    37.  


    GuiStuff is the simplest test class:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GUIStuff : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    17.  

    In the Mac editor (Unity 4.3.4) the scripts above work as expected, and the debug line prints the current toolbar selection correctly.

    In the Windows editor (Unity 4.3.4) everything works the first time (when a GUIStuff object is created), but as soon as you save the scene and reopen it, the debug line in SceneGUI always prints Selection 0: true Selection 1; false, independently of what's selected on the toolbar.

    Any ideas on what's going on?

    Cheers,
    Alex
     
    Last edited: Apr 13, 2014
  2. Game-Whiz

    Game-Whiz

    Joined:
    Nov 10, 2011
    Posts:
    122
    This is a bug in Unity, but it happened because I had two inspector windows, i.e. the GUIStuff object appeared in two inspectors at the same time. Apparently Unity GUI doesn't like that.