TextField focus issues

Hi.
I have complex GUI and there’re few textfields placed horizontally. Some of them could be hidden, and then shown again. So I found strage behaviour: control get focus and works fine, but it’s focus name wrong (it has name of hidden control).
I suppose it’s Unity bug. Anyone have ideas or suggestions?

Here is test code to show the problem.
Steps to reproduce:

  1. Show all textfields
  2. Hide 1st
  3. Focus 2rd
    You can see: selected 2nd control, but FocusControlName is 1st.

Code:

using UnityEngine;
using System.Collections;

public class TextFieldBugTest : MonoBehaviour {

	string[] text = new string[] { "text0", "text1", "text2" };
	bool[] show = new bool[] { true, true, true };
	
	void OnGUI() {
		// show textfields
		for (int i = 0; i < text.Length; i++)	{
			if (show[i]) {
				GUILayout.BeginHorizontal();
				// set control name
				GUI.SetNextControlName( "" + i );
				// show actually textfield
				// 'my control name' just to be sure, that it's correct control.
				text[ i ] = GUILayout.TextField( text[ i ] );
				GUILayout.Label ( "Control name " + i );
				GUILayout.EndHorizontal();
			}
		}
		//-------------------
		// functional buttons
		for (int i = 0; i < text.Length; i++) {
			GUILayout.BeginHorizontal();
			// button to show/hide textfield
			if (GUILayout.Button( show[i] ? "hide" : "show" ))
			{
				show[i] = !show[i];
			}
			// button to focus textfield
			if (GUILayout.Button( "focus " + i ) ) {
				GUI.FocusControl( "" + i );
			}
			GUILayout.EndHorizontal();
		}
		
		GUILayout.BeginHorizontal();
		// So... here we get incorrect result
		// I think textfield shifted on place of another one. Visual selection are correect and any operatin with also
		// But the name of focused control is wrong!
		GUILayout.Label( "Name of focused control: " + GUI.GetNameOfFocusedControl() );
		GUILayout.EndHorizontal();
	}
}
1 Like

Hi, welcome to the forum!

Please can you file a report for this using the Report Bug app?

Yeah, sure.
Want to know, maybe someone got same issue, and found workaround?

We’re running into the same problem here. Has there been any resolution or workaround to this? I can file a new bug report if needed.

Hi, usually I hate to bump threads that are old, especially ones that are 3 years old, but this one needs to be bumped again.

I’m having this exact issues, 3 years after it was first reported. Does anyone have a work around, or is there some way we can push to get a bug fix in the queue?

I can file another ticket if needed, too.

Three years old, and apparently the problem is still present.
Multiple text fields produce focus events for all of them.

any work arounds?