Search Unity

TextFields become soul mates and show the same text

Discussion in 'Immediate Mode GUI (IMGUI)' started by Zogg, Oct 4, 2011.

  1. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    158
    Hello,

    I'm currently coding a custom EditorWindow, and there is a strange bug which occurs occasionally : Two text fields in different windows show the same string, the text in the first text field is displayed in the second one as well, even during editing of said text. In the following screenshot, the text field in the second window displays the text "TestVisitRivals" from the first one, although it should contain a wholly different text :



    The windows are created with GUI.Window inside the EditorWindow's OnGUI function. Note that this is not a problem of two windows sharing the same ID (the window headers on the screenshot indicate their IDs 1006 and 1007). Also, I'm sure the two text fields do not share the same string variable.

    Note that the lower TextField contains visually the text of the first one but apparently not logically : When I select a third text field it snaps back to it's correct value. (Furthermore, if there were really "TestVisitRivals" in this field, the text would turn red, as text fields with the little top hat turn red iff they don't contain the name of an existing GameObject.)

    A detail which might be important: When I select the first or the second window, the corresponding text field is in edit mode (with grey rectangle and cursor). However, when I select a third window, it's text field is not in edit mode (looks like a label). It's as if the two text fields were in a certain manner simultanously in edit mode (but only the one in the selected window is grey with cursor).

    Unfortunately, this error only happens occasionally, and I've not yet found a way of reproducing it.

    Any idea where this bug comes from, and how to fix it?
     
    SombreErmine likes this.
  2. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    158
    Anybody ?
     
  3. AlteredReality

    AlteredReality

    Joined:
    Aug 15, 2011
    Posts:
    397
    I have actually had this same exact issue in the past! This was actually happening with EditorGUILayout.FloatField and EditorGUILayout.TextField with me. Were you able to find a solution? I decided to use GUILayout.TextField instead, although THAT has some very frustrating (and random) issue where sometimes the TextField in the GUI.Window becomes stuck, and you can't place the cursor past the first character.

    I am still trying to work out a way around both of these issues. If anyone has any info, that would be awesome!
     
  4. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    158
    I didn't find a proper solution, even replacing EditorGUILayout.[whatever]Field by GUILayout.TextField didn't work. When this happens, and in some situations (two windows with identical controls) it happens systematically - the focussed control (according to GUI.GetNameOfFocusedControl() ) is not the text field you are actually editing, but the "remote controlled" one. Even forcing the control to the correct field with GUI.FocusControl() doesn't work, the focus stays on the wrong field.

    However, I think I just found a workaround : When a panel isn't selected, I display a label instead of a text field. It's a dirty hack but at least it works. Funny thing is that GUI.GetNameOfFocusedControl() now returns the name of the label, although in theory a label can't be possibly focussed. But who cares ?

    (Well, actually, I care - I don't like having dirty hacks instead of proper solutions in my code. But a dirty hack that works is better than a proper code that doesn't work.)
     
    SombreErmine likes this.
  5. AlteredReality

    AlteredReality

    Joined:
    Aug 15, 2011
    Posts:
    397
    Thanks for the insight...that's actually a great workaround! Did you ever see the issue where you can't properly edit a text field? The issue being that you can't place the cursor past the first character.

    Unfortunately, for as much as I love Unity, the GUI system is the worst part of it. Alot of the work on Sage honestly felt like it went into dealing with crazy issues caused by the GUI system, and figuring out how to work around them. So in the end, like you, I had to give up on "clean and/or proper" code, and go with hacks and workarounds in order to get it to actually work properly.
     
  6. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Sounds as if this could be an issue with the fact that in c# strings are actualy reference types although in many instances they act like value types. If some confusion over this is the cause or not I wouldn't be able to say if the problem was in your code or Unity's but it might be worth just checking all your string assignments and where they are being passed as function parameters to be double sure that you've remebered they are reference types. Just an idea
     
  7. Zogg

    Zogg

    Joined:
    Mar 28, 2009
    Posts:
    158
    No, I've never seen this.

    That's a good idea, but I checked whether there was possibly the wrong string referenced : I modified the label before the text field such that it displays the very string used in the TextField - but when a text field was "magically linked" to the currently edited text field, label and text field showed different strings.

    Besides, by colouring the focussed text field green (focussed according to GUI.GetNameOfFocusedControl()), I clearly saw that the focussed (green) text field was not the one I was actually writing into - which shows that the problem was not limited to the content of the two fields, but lies in a disagreement between me and Unity about which text field has actually the focus.


    P.S.: As a sidenote, I learned that you can actually focus nothing with GUI.FocusControl( "" ) - but only if you put GUI.SetNextControlName( "" ); at the very end of OnGUI(). I found that funny. (However, it didn't help in my case.)
     
    Last edited: Mar 7, 2012
    LaireonGames and SombreErmine like this.
  8. SombreErmine

    SombreErmine

    Joined:
    Aug 11, 2014
    Posts:
    2
    This is super annoying. It's still an issue three years later in 5.1.1 (currently updating to 5.1.2 but I doubt it's on anybody's radar to fix). I'm guessing it's somehow related to OnGUIs and their different rendering states like Layout and Repaint, and also somehow related to the Focused input field. Beats me! I'd use something other than OnGUI for my custom editor windows if I knew how.

    It's happening to me inside a single EditorWindow's OnGUI call with a EditorGUILayout.TextField and EditorGUILayout.IntField. It's displaying the content from the TextField (a string) in the IntField (what should be a number) and vice versa.

    I basically created a "page" system where if it's Page 1 (currentPage == 1), it shows the TextField; otherwise, if it's Page 2, it shows the IntField.

    Displaying a different Label in-between the differing page draws doesn't work.

    However!! Using GUI.FocusControl("") to clear the focused control in-between the differing page draws worked for me! Thanks for the diligent testing of different workarounds!



    P.S. - It took me a lot of Google acrobatics to find this forum post (this is the only thing on the Internet I found experiencing the same thing as me...). So I'll add some keywords here like "TextField displaying wrong text" or "IntField showing TextField's content" or "IntField contains a string". Maybe Google will index them.
     
    Last edited: Jul 29, 2015
    LaireonGames likes this.
  9. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    I have this issue as well and the focus control did the trick for me. Has anyone raised a bug report? (P.S thanks Sombre I think your indexing helped)
     
  10. sonny-ad

    sonny-ad

    Joined:
    Jan 22, 2014
    Posts:
    4
    Hi
    I had the same issue with several EditorGUI.TextField (...) in different Windows. I just replaced it by GUI.TextField (...)
    And it seems to work as I wished for me.
     
  11. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Amazing this problem still exists 6 YEARS LATER.

    :'(
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Did anyone file a bug report?
     
  13. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    @karl_jones

    I've personally only had it happen once or twice when testing the UGUI functionality when it was relatively new while working on an Android project using that gui system. I thought it was user error at the time.

    <offtopic> I eventually went with a less intensive solution for Android development rather than calling "setActive" all the time for lots of ui elements (No idea why that's not built-in on some level... </offtopic>)

    Anyone else here having the issue more frequently -- would you mind filling a bug report please?
     
  14. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    ok.. this is super annoying. I'll make a little test project and submit a bug report if I can reproduce it in there. Not going to submit the actual editor code it is happening in now cause that project it too complicated.
     
  15. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Submitted a bug report (Case 927073) though I am not sure whether this is a bug or just me/us using this incorrectly. To test this I had to use a Button to change the active data set since it does not clear the focus of the input field like a Dropdown would for example.

    So it seems we are simply required to clear focus when it is not automatically done. Though, using the `GUI.FocusControl("");` method in my bigger project does not even clear/change the focus anyway so this "hack" does not work for me.

    Here is the test code.

    Code (CSharp):
    1. public class TestWindow1 : EditorWindow
    2. {
    3.     private static GUIContent GC_Field1 = new GUIContent("Field 1");
    4.     private static GUIContent GC_Field2 = new GUIContent("Field 2");
    5.     private static GUIContent GC_DataChange = new GUIContent("Change Data Group");
    6.  
    7.     public class Data
    8.     {
    9.         public string field1;
    10.         public string field2;
    11.     }
    12.  
    13.     private Data[] datas = { new Data(), new Data() };
    14.     private int activeData = 0;
    15.  
    16.     [MenuItem("TEST/Open Test Window 1")]
    17.     private static void ShowTestWindow1()
    18.     {
    19.         GetWindow<TestWindow1>("test1");
    20.     }
    21.  
    22.     private void OnGUI()
    23.     {
    24.         EditorGUILayout.Space();
    25.  
    26.         if (GUILayout.Button(GC_DataChange))
    27.         {
    28.             activeData = activeData == 0 ? 1 : 0;
    29.  
    30.             // doing this to force clear focus seems to help
    31.             //GUI.FocusControl("");
    32.         }
    33.  
    34.         EditorGUILayout.Space();
    35.         GUILayout.Label("Active data is: " + activeData);
    36.  
    37.         EditorGUILayout.Space();
    38.         datas[activeData].field1 = EditorGUILayout.TextField(GC_Field1, datas[activeData].field1);
    39.         datas[activeData].field2 = EditorGUILayout.TextField(GC_Field2, datas[activeData].field2);
    40.     }
    41. }
     
    Kumo-Kairo likes this.