GUI.GetNameOfFocusedControl() is not working correctly

Hi there,

I’m struggling with GUI when extending editor by custom functions.

I’ve found a really weird behavior which I am convinced might be a bug:

I am creating in runtime dynamic TextBoxes and assigning names to them. To do so, I have a List of my own objects and one attribute of their class is unique id I am assigning as a suffix to each textbox. I am then checking GUI.GetNameOfFocusedControl() to do contextual information over selected TextBox.

I have an interface for adding and deleting textboxes with incremental addition of ID’s so basically I can see that
the name goes like: Edit0, Edit1, Edit2, Edit3 etc.

In this instance GUI.GetNameOfFocusedControl() works great

I also have a button for deleting any TextBox of my preference and here comes the problem:

If I add fields Edit0, Edit1 and Edit2 and later on I delete Edit1, the name is correctly generated as Edit0 and Edit2
However when Edit2 gets focus it is still identified by GetNameOfFocusedControl as Edit1 as if it was somehow cached!

Is it something for a bug report or am I missing some Invalidate method to refresh the cache?

Code excerpt:

// This shows correctly Edit0, Edit1, Edit2 and later on Edit0, Edit2 when I delete Edit1 from the collection
Debug.Log(name)

GUI.SetNextControlName(name);
_node.ModifyLineByKey(w, GUILayout.TextField (a.LineText, GUILayout.MaxWidth(400f)));

elsewhere I simply call:

// This is broken. While it originally shows Edit0, Edit1,Edit2 when I delete Edit1 Focus on Edit2 claims it's Edit1
Debug.Log(GUI.GetNameOfFocusedControl());

Thanks in advance!

Jan

I have same issue with Unity 4.0.0f7.
Seems like a bug.
What Unity version do you have? Was it happening in v3?

It’s certainly a bug. I have dynamic GUIs that come and go as you select things, and every time I show a new set of controls, I generate new IDs for them. The old IDs are never reused. I call SetNameOfFocusedControl right before calling GUI.TextField, and it has the focus, that’s the name I should get back from GetNameOfFocusedControl (right?). But instead, I get back the name of some old control name that hasn’t been used for ages.

Has anybody filed this bug with Unity yet?

Also, this game causes my bug to be broken… I want to do stuff when the return key is pressed while the field has focus, or when the field loses focus, but I have no reliable way to tell when it has the focus. Can anyone suggest a workaround?

OK, I think I found a workaround, which I posted here. In brief, you can’t make your own unique control names; you need to reuse control names in the same way that Unity appears to do under the hood, which (apparently) you can do by basing your control names on GUIUtility.GetControlID.

    string controlName = "ValueFld" + GUIUtility.GetControlID(FocusType.Keyboard);
    GUI.SetNextControlName(controlName);
    value = GUI.TextField(readoutR, value, GUI.skin.GetStyle("Text"));
    bool hasFocus = (GUI.GetNameOfFocusedControl() == controlName);

Hope this helps somebody else work around this bug!

Hello,

This worked for me after hours of trying to figure it out.

Thanks,
jrDev

1 Like