Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EditorGUILayout.Popup has always the same value selected

Discussion in 'Scripting' started by Deecann, Sep 26, 2014.

  1. Deecann

    Deecann

    Joined:
    Mar 17, 2010
    Posts:
    93
    Got problem with custom inspector popup list ( drop down ).

    Code (CSharp):
    1. [CustomEditor(typeof(FieldPrefab))]
    2. public class CustomFieldInspector : Editor {
    3.  
    4.     public int index = 0;
    5.     public Texture TextureColor;
    6.  
    7.     public override void OnInspectorGUI() {
    8.         string[] options = new string[]{ "Red", "Yellow", "Green" };
    9.         var MyObject = target as FieldPrefab;
    10.  
    11.         index = EditorGUILayout.Popup("Field type:", index, options, EditorStyles.popup);
    12.         switch(index) {
    13.             case 0: TextureColor = (Texture)Resources.Load("Textures/Red"); break;
    14.             case 1: TextureColor = (Texture)Resources.Load("Textures/Yellow"); break;
    15.             case 2: TextureColor = (Texture)Resources.Load("Textures/Blue"); break;
    16.         }
    17.         MyObject.renderer.sharedMaterial.SetTexture("_MainTex", TextureColor);
    18.  
    19.     }
    20. }
    Changing color works fine, but when I change the color of one object, on editor window take another and come back to this first he get's again first texture from list. So, the point is what is the method for catching current field state and set as value.
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    I don't think you can save a variable in a class derived from Editor like this. You'll have to create an integer called index in your FieldPrefab-script and access it from your custom inspector script.

    Code (CSharp):
    1. myObject.index = EditorGUILayout.Popup("Field type:", myObject.index, options, EditorStyles.popup);
     
    _uso212 and bhaptics like this.
  3. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    256
    I seem to have the same problem, but your suggestion doesn't seem to work :(
    My Code:
    myClass.SelectedGroupIndex = EditorGUILayout.Popup(myClass.SelectedGroupIndex, myClass.GroupNames);


    Index always stays 0. I found out that after this line the index is correct, but in the next OnGUI() iteration everything is 0 again. And yes this is the only code that touches the classes SelectedGroupIndex. I'm lost.

    Plot Twist: The class had been overridden the whole time. ._.
     
    Last edited: Oct 31, 2020