I am building a chat window and everything is going pretty good however I have run into a small issue now. I have my chat window and a text field to enter messages into and if the messages become to big for the chat window to show them all, a vertical scrollbar appears next the the chat window to scroll through all the messages. The issue is that when the vertical scrollbar is first rendered, the text field loses its focus so that I have to re-click in it to start typing again. What could be causing this effect?
I’m curious if you solved this issue?
I’m having the same problem - the focus and cursor dissapear from TextField when some other GUI element is rendered.
Found the solution here: http://forum.unity3d.com/viewtopic.php?t=28242&highlight=gui+text+focus
Doesn’t explain why it’s losing focus in the first place. I have a similar problem… however, in my case I’m not even ‘enabling’ the scroll bar.
I can’t blindly call FocusControl in this case, even when the focused control is “” (which is the way this occurs for me), because it is a valid state in our game for no control to be active (at which point, keypresses may act as ‘hotkeys’)
Still don’t have the whole story, but I know how to work around this now.
When the number of controls on the screen changed, it appears that the focus was getting automatically reset to the first item. Drawing the text box as the first control on the screen seems to work. Alternatively, if ordering is important for other reasons, I suspect you could create a ‘dummy’ named control at the start of the OnGUI and then do something like:
if(GUI.GetNameOfFocusedControl() == “dummyControl”)
{
GUI.FocusControl(“theOneIReallyWant”);
}
Making it my first control worked for me, though.