Search Unity

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

Discussion in 'Scripting' started by csciguy, Dec 27, 2006.

  1. csciguy

    csciguy

    Joined:
    Dec 13, 2006
    Posts:
    22
    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
    Code (csharp):
    1.  
    2. function Update () {
    3.     for (var c : char in Input.inputString) {
    4.         // Backspace - Remove the last character
    5.         if (c == "\b") {
    6.             if (guiText.text.Length != 0)
    7.                 this.guiText.text = guiText.text.Substring(0, guiText.text.Length - 1);
    8.         }
    9.         // End of entry
    10.         else if (c == "\n") {
    11.             TextValue(this.guiText.text);
    12.         }
    13.         // Normal text input - just append to the end
    14.         else {
    15.             this.guiText.text += c;
    16.         }
    17.     }
    18. }
    19.  
    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.
    Code (csharp):
    1.  
    2. function MakeGuiText(textName, textValue : String,positionVector, textOffset, textScript : String){
    3.  
    4.     var itemTextElement : GameObject = new GameObject(textName);
    5.     var textElement : GUIText = itemTextElement.AddComponent("GUIText");
    6.     textElement.text = textValue;
    7.     if(textScript!=null){
    8.         itemTextElement.AddComponent(textScript);      
    9.     }
    10.     itemTextElement.transform.localScale = Vector3.zero;
    11.     itemTextElement.transform.localPosition  =positionVector;
    12.     itemTextElement.guiText.pixelOffset = textOffset;
    13.     return itemTextElement;
    14. }
    15.  
    So a call to create these two elements would be...
    Code (csharp):
    1.  
    2. var inputKey_rotate_left = MakeGuiText("inputKey_rotate_left","Q", positionVectorAt2_0,settingsInputConfigurationKey_Value_rotate_left_PixelOffset,"textHighlightScript");
    3. var inputKey_rotate_right = MakeGuiText("inputKey_rotate_right","W", positionVectorAt2_0,settingsInputConfigurationKey_Value_rotate_right_PixelOffset,"textHighlightScript");
    4.  
    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.
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i'm guessing your var c needs to be a private variable.
     
  3. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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:
    Code (csharp):
    1.  
    2. static var typingIntoThis : GameObject = null;
    3.  
    4. function Awake() {
    5. typingIntoThis=null;
    6. }
    7. function OnMouseDown() {
    8. typingIntoThis=gameObject;
    9. }
    10. function Update () {
    11. if (gameObject == typingIntoThis) {
    12.     for (var c : char in Input.inputString) {
    13. . . .
    14.     }
    15. }
    16. }
    17.  
    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) )
     
  5. csciguy

    csciguy

    Joined:
    Dec 13, 2006
    Posts:
    22
    Thanks for the help guys. Using a static variable solved the problem.