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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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,724
    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:
    36,780
    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,724
    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.