Side effect of attaching a script to multiple gui text objs?

A little preface before this question…

I have multiple game objects in my scene. Each game object has a gui text element attached. Each text element has different text.

So, for example, I have 2 game objects and one has the text value of Q and the other has a text value of W.

Each of these items has the following script attached to capture user input. (borrowed from the API)

inside textHighlightScript

function Update () {
    for (var c : char in Input.inputString) {
        // Backspace - Remove the last character
        if (c == "\b") {
            if (guiText.text.Length != 0)
                this.guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
        }
        // End of entry
        else if (c == "\n") {
            TextValue(this.guiText.text);
        }
        // Normal text input - just append to the end
        else {
            this.guiText.text += c;
        }
    }
}

When I click on one of the items, Q for example, and type, I can append whatever the user types to this item. If the user in this example types WERTY, then the element with Q would display “QWERTY”.

The problem is that the element with “W” also displays “WWERTY”, even though I clicked on the “Q” element.

Each object is created via this method.

function MakeGuiText(textName, textValue : String,positionVector, textOffset, textScript : String){

	var itemTextElement : GameObject = new GameObject(textName);
 	var textElement : GUIText = itemTextElement.AddComponent("GUIText");
 	textElement.text = textValue;
 	if(textScript!=null){
 		itemTextElement.AddComponent(textScript); 		
 	}
 	itemTextElement.transform.localScale = Vector3.zero;
 	itemTextElement.transform.localPosition  =positionVector;
 	itemTextElement.guiText.pixelOffset = textOffset;
 	return itemTextElement;
}

So a call to create these two elements would be…

var inputKey_rotate_left = MakeGuiText("inputKey_rotate_left","Q", positionVectorAt2_0,settingsInputConfigurationKey_Value_rotate_left_PixelOffset,"textHighlightScript");
var inputKey_rotate_right = MakeGuiText("inputKey_rotate_right","W", positionVectorAt2_0,settingsInputConfigurationKey_Value_rotate_right_PixelOffset,"textHighlightScript");

My question is this. Why does a script attach to one element, affect other elements with that script attached? Am I wrong to assume that each time I create a seperate game object, they have their own instance of the script attached to them?

Any help is appreciated.

i’m guessing your var c needs to be a private variable.

If the script attached to the two GUITexts contain the Update method above, then both will react to keystrokes. Clicking on one or the other GUIText object will not automatically disable the update on the other one.

You will have to detect the click and implement some sort of keyboard focus.

Expanding on what freyr said, I use basically the same concept (albeit entering single characters instead of strings) in BotBuilder. What you need is a way to let each object know, “You are/aren’t being typed in to”. IMO the cleanest way to do that is via a static variable:

static var typingIntoThis : GameObject = null;

function Awake() {
typingIntoThis=null;
}
function OnMouseDown() {
typingIntoThis=gameObject;
}
function Update () { 
if (gameObject == typingIntoThis) {
    for (var c : char in Input.inputString) { 
. . .
    } 
}
}

The static variable is shared between all instances of the script; all scripts will see the changes to this variable made by any one script. If you wish to disable all of them (say, after the user types something and presses enter), just set typingIntoThis to null.

(I would also recommend you indicate visually which one is being typed into, e.g. by changing its color depending on (gameObject == typingIntoThis) )

Thanks for the help guys. Using a static variable solved the problem.