x


For loop on text without effecting each other

I'm trying to create a certain number of text areas based on the length of a word using a for loop. It works, however when i edit one text area, all of them get edited. Does anyone know how I can make it so it doesnt effect all the text areas made in the for loop?

static var underScore : String = "_";

var guessTextStyle : GUIStyle;

var word : String;

function OnGUI() {

    textPosY = Screen.height - 50;
    textPosX = 25;
    i = 0;

    for(i = 0; i <= word.length; i++)
    {
       underScore = GUI.TextArea(Rect(textPosX,textPosY,50,50),underScore, 1, guessTextStyle);
       textPosX += 75;
    }

}
more ▼

asked Apr 17 '12 at 08:12 PM

Kag359six gravatar image

Kag359six
102 20 31 33

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You need each text to have its own input string. You kept changing the same one.

static var underScore : String[];// = "_";

var guessTextStyle : GUIStyle;

var word : String;
private var lastWord : String;

function Start()
{
    UpdateUnderScoreWhenNewWord();
}
function Update()
{
    // I urge you to call UpdateUnderScoreWhenNewWord() only when you need - string          
    // comparison is costly
    if ( word != lastWord )
      UpdateUnderScoreWhenNewWord();
}

// Currently - this clears word!!!!!
function UpdateUnderScoreWhenNewWord()
{
   lastWord = word;
   underScore = new String[word.Length];
   for(var i=0; i<underScore.Length; i++)
   {
      underScore[i] = "_";
   }
}

function OnGUI() {

    textPosY = Screen.height - 50;
    textPosX = 25;
    i = 0;

    for(i = 0; i <= word.length; i++)
    {
       underScore[i] = GUI.TextArea(Rect(textPosX,textPosY,50,50),underScore[i], 1, guessTextStyle);
       textPosX += 75;
    }

}
more ▼

answered Apr 17 '12 at 08:26 PM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x552
x418
x95
x62
x55

asked: Apr 17 '12 at 08:12 PM

Seen: 448 times

Last Updated: Apr 17 '12 at 09:19 PM