How to change GUIText color randomly ?

In my game I place GUIText many times on screen and I want its color is changed randomly. Example:


  • first time appear: Red.
  • second time appear: Green.
  • Third time appear: Blue.

I try to use this solution:
GUIText.material.color = new Color (Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255)) but it not work.
How can I do this ?
Thanks for your help.

Well two things:

  1. Color() takes three floats between 0-1 not 0-255. So you should probably do Random.Range(0, 255)/255) for each color.

  2. That’s going to work, but I imagine that most colors it generates are brownish. I’d probably create an array of colors of size 16 or so, and manually assign each color to the array. Then generate a random index into that array, so that you get colors that are useful:

    Color colors = {Color.red, Color.cyan, Color.yellow }; //etc

    int indx= Random.Range(0,colors.Length);

    GUIText.material.color = colors[indx];