x


show text randomly

Hello. I have a question. How can I show texts randomly? For example if the player press a button than show randomly "TEXT1" or "Text2" or "Text3"?

How can I do that ? please help

more ▼

asked Apr 29 '12 at 01:58 PM

worldofcars gravatar image

worldofcars
125 16 24 30

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

2 answers: sort voted first
public class randommessage : MonoBehaviour {
    int message = 0;   

    public void Start()
    {
       message = Random.Range(1,3);
    }

    public void OnGUI()
    {
       if(message == 1)
         GUI.Label(new Rect(Screen.width/2, Screen.height/2, 500, 30), "Text1");
       if(message == 2)
         GUI.Label(new Rect(Screen.width/2, Screen.height/2, 500, 30), "Text2");
       if(message == 3)
         GUI.Label(new Rect(Screen.width/2, Screen.height/2, 500, 30), "Text3");
    }
}
more ▼

answered Apr 29 '12 at 02:18 PM

DJSwiti gravatar image

DJSwiti
105 6 8 9

For added fun, try replacing Start with Update!

Apr 29 '12 at 02:20 PM syclamoth

Thanks for the help guys but I found it out myself;)

Apr 29 '12 at 02:22 PM worldofcars
(comments are locked)
10|3000 characters needed characters left

Try something like this:

string[] texts = new string[]{"Text1", "Text2", "TEXT THREE"};

Then, when you want to get a random one, use this line:

string currentText = texts[Random.Range(0, texts.Length)];
more ▼

answered Apr 29 '12 at 02:19 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

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

x570
x550
x36

asked: Apr 29 '12 at 01:58 PM

Seen: 536 times

Last Updated: Apr 29 '12 at 02:32 PM