x


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);
	
		}
		
		}
	
}
more ▼

asked May 12 '12 at 12:55 AM

MetalLord gravatar image

MetalLord
18 5 14 15

Set o and i as public, then observe how they behave in the inspector during runtime. There is probably something going here.

May 12 '12 at 01:41 AM Berenger
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I do see one thing:

tex1.text = texto1 [+1];
tex2.text = texto2 [+1];

Instead of [1], you might want [o]?

more ▼

answered May 12 '12 at 01:51 AM

rutter gravatar image

rutter
5.2k 2 11

It worked,didn't know variables can be use to call array elements.

May 12 '12 at 01:58 AM MetalLord

You can use any expression to index an array.

May 12 '12 at 02:18 AM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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);
    }
    ...

more ▼

answered May 12 '12 at 01:55 AM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

(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:

x3684
x419
x313

asked: May 12 '12 at 12:55 AM

Seen: 589 times

Last Updated: May 12 '12 at 02:18 AM