How to define in which text field the cursors is located

Hi,

I have two different textfields.
When I start typing however, no matter where the cursor is located,
both text fields receive the same input.

If anyone could point me in the right direction, it would be greatly appreciated.
Also, both text fields, have different var names.

Here’s the code I am using;

private var mytarget: GameObject;
private var variable: float;
var num: int;
 var c : char;
 var mystring : String = "";

var myfloat : float = 0.0;

function Start(){
mytarget = GameObject.Find("Cube");

}

function Update () {

for (c in Input.inputString)
	{
	if(c== "\b"[0])
		{
		if (guiText.text.Length !=0)
		guiText.text = guiText.text.Substring(0, guiText.text.Length -1);
		}
	else if(c =="

"[0]|| c== “\r”[0])
{
print (guiText.text);
num = parseInt (guiText.text);
}
else
{
guiText.text +=c;
}
}
}

function OnGUI()
{
GUI.TextField(Rect(50, 50, 200, 20), guiText.text);
}

This isn't the usual way of managing text fields in UnityGUI. Why aren't you doing it the normal way?

guiText.text = GUI.TextField(Rect(50, 50, 200, 20), guiText.text);

In any case, you can use Rect.Contains to check for the screen coordinates of the mouse if you are set on doing things in the strange way you are doing here. Keep a rect

var textRect : Rect = Rect(50, 50, 200, 20);

and then check

var correctedMousePosition : Vector2 = Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
if(textRect.Contains(correctedMousePosition))
{
    // Do the rest!
}

EDIT: A more sophisticated way to do this would be to use GUI.GetNameOfFocusedControl. This way, you could use the GUI's keyboard and mouse focusing system to manage which one you are passing input to through your special input parser!

Thanks for your reply.
After a few more trials and errors, I managed to get it to work,
using a boolean and the GUI.GetNameOfFocusedControl function. Works great !

Here’s the result
var num_a: int;
var c_a : char;
var ici_a: boolean = false;
var mystring_a : String = “Enter RPM here”;

function Start(){
guiText.text = mystring_a;

}

function Update () {
if (ici_a == true)
{
	for (c_a in Input.inputString)
		{
		if(c_a== "\b"[0])
			{
			if (guiText.text.Length !=0)
			guiText.text = guiText.text.Substring(0, guiText.text.Length -1);
			}
		else if(c_a =="

"[0]|| c_a== “\r”[0])
{
print (guiText.text);
num_a = parseInt (guiText.text);
}
else
{
guiText.text +=c_a;
}
}
}
}

function OnGUI()
{
GUI.SetNextControlName ("TXT_rpm");
GUI.TextField(Rect(50, 50, 500, 20), guiText.text);

if (GUI.GetNameOfFocusedControl () == "TXT_rpm")
	{
	ici_a = true;
	}

if (GUI.GetNameOfFocusedControl () != "TXT_rpm")
	{
	ici_a = false;
	guiText.text = mystring_a;
	}

if (GUI.GetNameOfFocusedControl () == "TXT_rpm" & Input.GetMouseButtonDown(0))
	{
	guiText.text="";
	}
},