- Home /
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;
}
}
Comment
Best Answer
Answer by GuyTidhar · Apr 17, 2012 at 08:26 PM
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;
}
}