Clear text field and allow user to return to keyboard control


I'm a newbie in Unity and I tried to search about this on google before, but I couldn't find the answer that I wanted.

I created a text field which will be use for searching. When the program started, user will be able to control an object in the screen, then when the user click on the text field shown on the screen, and type some alphabets, it'll effect the keyboard control. So, I've put a condition to check if my text field is empty and if it's empty then allow keyboard control, else any key entered will be inputted to the text field without affecting the camera. But, I cannot make the text field to clear its value and return to keyboard control.

This is my piece of code where I created my text field:

function create_text_field() { var gameObject : GameObject = GameObject.Find("First Person Controller Prefab"); var controlCamera : CameraMovement = gameObject.GetComponent("CameraMovement");

text_string = GUI.TextField(Rect(0, 0, 150, 20), text_string, 60);
if (Input.GetKeyDown("enter"))
    text_string = "";

if (text_string != "") {
    controlCamera.stopInput = true;
else {
    controlCamera.stopInput = false;


stopInput is declared false at the beginning. Even though I set text_string = ""; -- the text field won't be cleared when I press enter.

How can I check if text field is activated (user click on it, and enter some value)? How can I enable or disable the blinking cursor when the text field is activated?

Thank you...

Look in the scripting reference for “GUI.SetNextControlName” and “GUI.GetNameOfFocusedControl”.
You could use those two function to detect if your textfeild is currently in use.
Hope that’s what you need!

var text : String = “”;
var initext : String = “Enter a text”;
var texterased : boolean = false;

Function Awake (){
text = initext;
Function OnGUI (){
GUI.SetNextControlName ("mytext");
GUI.TextField (Rect(0, 0, 150, 20), text, 60);
if (GUI.GetNameOfFocusedControl () == "mytext"){
      if (texterased == false){
             text = "";
             texterased = true;
else if (text != initext){
       text = initext;