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

Question If statements don't work under 'if (EditorGUI.EndChangeCheck())'

Discussion in 'Scripting' started by KimberleyC, Aug 9, 2021.

  1. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20
    Hi, I'm running into an issue where the code doesn't seem to execute under if statements within 'if (EditorGUI.EndChangeCheck())'; the thing is, the codes under both conditions work fine without the if statement. Funnily enough, Debug.Log() is the only thing that seems to execute perfectly.


    Code (CSharp):
    1.             Vector3 frameScale = txtboard.frame.transform.localScale;
    2.             Vector3 boardScale = txtboard.board.transform.localScale;
    3.             float frameThickness;
    4.  
    5.             EditorGUI.BeginChangeCheck();
    6.             int frameSizeX = EditorGUILayout.IntSlider("X", txtboard.FrameSizeX, 1, 5);
    7.             if (EditorGUI.EndChangeCheck())
    8.             {
    9.                 txtboard.FrameSizeX = frameSizeX;
    10.  
    11.                 if (txtboard.transform.localScale.x <= txtboard.transform.localScale.y)
    12.                 {
    13.                     Debug.Log("this is logged");
    14.                     frameThickness = boardScale.x + (boardScale.x * (frameSizeX * 0.1f));
    15.                    
    16.                 } else
    17.                 {
    18.                     Debug.Log("this is logged as well");
    19.                     frameThickness = boardScale.y + (boardScale.y * (frameSizeX * 0.1f));
    20.                 }
    21.  
    22.                 frameScale = new Vector3((frameScale.x + frameThickness), (frameScale.y + frameThickness), frameScale.z);
    23.             }

    Can someone help me with this?
     
  2. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    135
    Do you have tested the example which is available on the EditorGUI.EndChangeCheck main page?
    Are you in the OnGUI section?
     
  3. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20
    Yes, I modified the code this way
    Code (CSharp):
    1.                 if (sliderValue < 0.5)
    2.                 {
    3.                     labelText = sliderValue.ToString();
    4.                 } else
    5.                 {
    6.                     labelText = "greater than 0.5!";
    7.                     EditorGUILayout.LabelField("new GUI element test", labelText);
    8.                 }
    EditorGUILayout.LabelField("new GUI element test", labelText); didn't work but labelText = "greater than 0.5!"; did

    I'm writing this in OnInspectorGUI()
     
  4. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    135
    Seems to me correct :/
    Do you have tested your sliderValue in a Debug.Log info so as see if it fluctuates?
     
  5. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20
    Exactly :(
    Ah, sorry what do you mean by that?
     
  6. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    135
    If I was struggling with a similar problem, having a statement condition using a single value, I would put a Debug.Log help instruction out of this condition, just to see if there is something wrong somewhere (before our condition if). I would like to know if this value fluctuates. This is what I meant...
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  8. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20
    Oh, I see, thanks for the clarification!
    I actually overcame the issue by using Mathf.Min() just before BeginChangeCheck. :p