Search Unity

Unity's version control component has been upgraded to Plastic SCM.

[Solved] Error: Editor variables returning different results in two different functions

Discussion in 'Unity Collaborate' started by paladinrose, Dec 11, 2016.

  1. paladinrose

    paladinrose

    Joined:
    Aug 1, 2010
    Posts:
    11
    I'm not sure if my problem is directly related to collaborate, but I know that it started immediately following an update to 5.5 Collaborate Beta. Some, but not all, of my teammates, also now updated to 5.5, are NOT having the same issues.

    So, here's my basic problem. My custom editor is using both OnInspectorGUI and OnSceneGUI.

    The editor itself has several values - mostly integers - for holding editor state information. Things like which tab is selected, or which options are selected in a list, things like that. When those values are updated, OnSceneGUI reports no change in them. Here is a simplified version that is having the same problem:

    Code (CSharp):
    1. public class ProblemFinder : MonoBehaviour {
    2.      // Tried containing variables on editor target, instead of editor, but got the same result.
    3.      // public int testInt = -1;
    4.      // Interestingly, using a Property getsetter, like this, DOES work.
    5.      // public int TestInt { get {return testInt; } set {testInt = value; } }
    6.      // public string[] testButtons = new string[] { "Test 1", "Bob", "Blueberry" };
    7. }
    and in the Editor folder:
    Code (CSharp):
    1. [CustomEditor (typeof(ProblemFinder))]
    2. public class ProblemFinderEditor : Editor {
    3.      int testInt = -1, currentInt = -1;
    4.      string[] testButtons = new string[] { "Test 1", "Bob", "Blueberry" };
    5.      public override void OnInspectorGUI()
    6.      {
    7.          Debug.Log("Test Int Value: " + testInt.ToString());
    8.          for (int i = 0; i < testButtons.Length; i++)
    9.          {
    10.            
    11.              if (currentInt != i && GUILayout.Button(testButtons[i]))
    12.              {
    13.                  testInt = i;
    14.                  Debug.Log("Clicked " + testInt.ToString() + ". " + testButtons[i]);
    15.                  SceneView.RepaintAll();
    16.              }
    17.              else if(currentInt ==i)
    18.              {
    19.                  GUILayout.Label(testButtons[i]);
    20.              }
    21.          }
    22.      }
    23.      public void OnSceneGUI()
    24.      {
    25.          Debug.Log("Scene View Updated: " + testInt.ToString());
    26.          if (testInt != -1)
    27.          {
    28.              // I never see this message.  testInt ALWAYS == -1 in this function
    29.              Debug.Log("Test Int = " + testInt.ToString());
    30.              currentInt = testInt;
    31.              testInt = -1;
    32.          }
    33.      }
    34. }
    I see the three buttons I would expect, in the Inspector: Test 1, Bob, and Blueberry.

    Clicking Blueberry returns the following:


    Clicked 2. Blueberry

    Scene View Updated: -1


    My initial debug comment, in OnInspectorGUI confirms that testInt HOLDS its value of 2.

    Even so, OnSceneGUI ALWAYS returns it with a value of -1. Referencing an int value on the editor target also returns two different results, depending on which function looks at it. But, interestingly, if I get or set the int through a Property, it works. Anyone have any idea why the others don't?
     
    IgorAherne likes this.
  2. bradunity

    bradunity

    Joined:
    Nov 12, 2013
    Posts:
    195
    This doesn't sound like something Collab would introduce. Possibly might relate to sharing a project among your team with mixed editor versions? I'll also ask around internally to see if others might have better insight into your issue...
     
  3. paladinrose

    paladinrose

    Joined:
    Aug 1, 2010
    Posts:
    11
    I had also asked about this on answers. One suggestion was that I check the Instance ID in both OnInspectorGUI and OnSceneGUI. Sure enough, I'm getting different instance ids between them. So Unity seems to be treating it as two separate editors. One for the Inspector and another for the Scene View. I'm exploring the possibility of getting the variables directly from the editor target. Will post my results when I've had a chance to test it.
     
  4. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Last edited: Dec 27, 2016
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Solved it. Unity still doesn't work well with multiple screens. Got top right corner of the window -> Layout -> default. Save and re-launch Unity.

    Booom!! everything gets deleted. Just kidding, - it solves the issue & we now get only 1 instance of editor
     
  6. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I suppose we could also use a slightly hacky way:
    Code (CSharp):
    1. //OnEnable function of our problematic editor
    2. void OnEnable(){
    3.       var remainingBuggedEditors  = FindObjectsOfType<MyEditor>();
    4.       foreach(var editor in remainingBuggedEditors) {
    5.           if(editor == this){
    6.                continue;
    7.            }
    8.           Editor.DestroyImmediate(editor);
    9.      }//end foreach
    10. }