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.