Search Unity

Question [Editor] Intfield does not change

Discussion in 'Scripting' started by CoffeeTR, Aug 3, 2020.

  1. CoffeeTR

    CoffeeTR

    Joined:
    Jun 24, 2015
    Posts:
    7






    I can not change

    (Unity 2020.1)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's a local variable so it won't persist between OnInspectorGUI() calls. You're creating it newly every time and giving the value 2 every time. Make it an instance variable and only initialize it once:
    Code (CSharp):
    1. public class EditorTarget : Editor {
    2.   int select = 2;
    3.  
    4.   public override void OnInspectorGUI() {
    5.     select = EditorGUILayout.IntSlider(select, 0, 5);
     
    CoffeeTR likes this.
  3. CoffeeTR

    CoffeeTR

    Joined:
    Jun 24, 2015
    Posts:
    7

    Thank you so much
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Line 11 in the top window declares select as a local variable and initializes it to 2, ever frame that this inspector runs.

    Move the variable out to a member in Target, then access it via the
    target
    variable in the editor window.

    CAUTION: using Target as your classname in this context may be very confusing! Read more here:

    https://docs.unity3d.com/ScriptReference/Editor.html

    Note the Editor class has a .target variable too.
     
    CoffeeTR and PraetorBlue like this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Ah @Kurt-Dekker is right about the target stuff. Having a variable on the Editor class itself will work in the GUI but it won't actually become accessible from your Target script (if that's what you're going for...)
     
    Kurt-Dekker likes this.