- Home /
Filling GuiText with contents from a string array
I wanted a simple system to display two lines of text,and every time i press the space bar,change the two lines to the next ones in a array,i assigned 6 lines in the inpector,but after i press the key for the second time,it load the next scene,without all the lines showing.
using UnityEngine; using System.Collections; public class sistemaconversa : MonoBehaviour { public string [] texto1; public string [] texto2; public GUIText tex1; public GUIText tex2; int i = 0; int o = 0; void Start () { i = texto1.Length; tex1.text = texto1 [0]; tex2.text = texto2 [0]; }
// Update is called once per frame
void Update () {
if(Input.GetKeyUp("space"))
{
o +=1;
tex1.text = texto1 [+1];
tex2.text = texto2 [+1];
}
if(o > i ) { Application.LoadLevel(Application.loadedLevel+1);
}
}
}
Set o and i as public, then observe how they behave in the inspector during runtime. There is probably something going here.
Answer by rutter · May 12, 2012 at 01:51 AM
I do see one thing:
tex1.text = texto1 [+1];
tex2.text = texto2 [+1];
Instead of [1]
, you might want [o]
?
It worked,didn't know variables can be use to call array elements.
Answer by aldonaletto · May 12, 2012 at 01:55 AM
You're always showing the same messages (texto1[1] and texto2[1]). You should use the index o, and pass to LoadLevel when o gets == i (arrays range from 0 to Length-1):
... if(Input.GetKeyUp("space")){ tex1.text = texto1 [o]; // display the messages index o... tex2.text = texto2 [o]; o += 1; // then increment the index } if ( o >= i ){ // when o >= length, load level: Application.LoadLevel(Application.loadedLevel+1); } ...