- Home /
 
 
               Question by 
               leafclover · Jun 02, 2015 at 11:22 AM · 
                guilistrandom.range  
              
 
              Can't prevent same color random generation.
Hello. I'm trying to generate bgcolor and textcolor using a list filled with color codes. The colors will be updated every seconds. What i get is bgcolors and textcolors generated same in many cases so the text can't be read. Here is my code:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ColorText : MonoBehaviour {
     public GUISkin mySkin;
     public GameObject obj;
     private Color clr;
     private Color txtClr;
     private string clrName;
 
     List<Color> colorList = new List<Color>(){
         new Color(0,0,255,255), // mavi
         new Color(165,42,42,255), // kahverengi
         new Color(128,128,128,255), // gri (192,192,192 gümüş)
         new Color(0,128,0,255), // yeşil
         new Color(128,0,0,255), // bordo
         new Color(255,165,0,255), // turuncu
         new Color(255,192,203,255), // pembe
         new Color(128,0,128,255), // mor
         new Color(255,0,0,255), // kırmızı
         new Color(64,224,208,255), // turkuaz
         new Color(255,255,0,255), // sarı
         new Color(255,255,255,255), // beyaz
         new Color(0,0,0,255), // siyah
     };
 
     /*Color[] colorList = new Color[13]{
         new Color(0,0,255,255), // mavi
         new Color(165,42,42,255), // kahverengi
         new Color(192,192,192,255), // gri (192,192,192 gümüş)
         new Color(0,128,0,255), // yeşil
         new Color(128,0,0,255), // bordo
         new Color(255,165,0,255), // turuncu
         new Color(255,192,203,255), // pembe
         new Color(128,0,128,255), // mor
         new Color(255,0,0,255), // kırmızı
         new Color(64,224,208,255), // turkuaz
         new Color(255,255,0,255), // sarı
         new Color(255,255,255,255), // beyaz
         new Color(0,0,0,255), // siyah
     };*/
 
     /*List<Color> colorAlp = new List<Color>(){
         new Color(0, 0, 255, .1f), // mavi
         new Color(165,42,42, .1f), // kahverengi
         new Color(128,128,128, .1f), // gri (192,192,192 gümüş)
         new Color(0,128,0, .1f), // yeşil
         new Color(128,0,0, .1f), // bordo
         new Color(255,165,0, .1f), // turuncu
         new Color(255,192,203, .1f), // pembe
         new Color(128,0,128, .1f), // mor
         new Color(255,0,0, .1f), // kırmızı
         new Color(64,224,208, .1f), // turkuaz
         new Color(255,255,0, .1f), // sarı
         new Color(255,255,255, .1f), // beyaz
         new Color(0,0,0, .1f), // siyah
     };*/
 
     /*string[] colorNames = new string[13]{
         "Mavi",
         "Kahverengi",
         "Gri", // gri (192,192,192 gümüş)
         "Yeşil", // yeşil
         "Bordo", // bordo
         "Turuncu", // turuncu
         "Pembe", // pembe
         "Mor", // mor
         "Kırmızı", // kırmızı
         "Turkuaz", // turkuaz
         "Sarı", // sarı
         "Beyaz", // beyaz
         "Siyah", // siyah
     };*/
 
     List<string> colorNames = new List<string>(){
         "Mavi",
         "Kahverengi",
         "Gri", // gri (192,192,192 gümüş)
         "Yeşil", // yeşil
         "Bordo", // bordo
         "Turuncu", // turuncu
         "Pembe", // pembe
         "Mor", // mor
         "Kırmızı", // kırmızı
         "Turkuaz", // turkuaz
         "Sarı", // sarı
         "Beyaz", // beyaz
         "Siyah", // siyah
     };
     
 
     // Use this for initialization
     void Start () {
         //obj = GameObject.Find ("a");
         InvokeRepeating ("changeColors", 0, 1f);
         //obj.GetComponent<Renderer> ().material.color = colorList [Random.Range (0, colorList.Count)];
     }
 
     /*void changeColors(){
         int a, b;
         a = (int)Random.Range (0, colorList.Length);
         b = (int)Random.Range (0, colorList.Length);
 
 
 
         if (a == b) {
             do{
                 a = (int)Random.Range (0, colorList.Length);
                 b = (int)Random.Range (0, colorList.Length);
             }while(a == b);
             clr = colorList [a];
             txtClr = colorList [b];
             clrName = colorNames [Random.Range (0, colorNames.Length)];
 
         } else {
             clr = colorList [a];
             txtClr = colorList [b];
             clrName = colorNames [Random.Range (0, colorNames.Length)];
         }
     }*/
 
 
 
     void changeColors()
     {
         List<Color> tempColors = new List<Color>(colorList);
 
         // Get the first random index.
         int a = (int)Random.Range (0, tempColors.Count);  
         // Assign the colour.
         clr = tempColors[a];  
         // Remove the colour from the list so that it is impossible to select the same again.
         tempColors.RemoveAt(a); 
         // Get second index.
         int b = (int)Random.Range (0, tempColors.Count); 
         // Assign second colour.
         txtClr = tempColors[b];
         clrName = colorNames [Random.Range (0, colorNames.Count)];
     }
 
 
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnGUI() {
         GUI.skin = mySkin;
         mySkin.box.fontStyle = FontStyle.Bold;
         mySkin.box.normal.textColor = txtClr;
         //GUI.backgroundColor = Color.blue;
 
         Texture2D texture = new Texture2D(1, 1);
         //Set new texture as background
         mySkin.box.normal.background = texture;
         //Set the texture color
         texture.SetPixel(1,1,clr);
         // Apply all SetPixel calls
         texture.Apply();
 
         /*float scalex = (float)(Screen.width) / 480.0f;
         float scaley = (float)(Screen.height) / 800.0f;
         GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scalex, scaley, 1));*/
 
 
         GUI.Box(new Rect(Screen.width/2 - 400,10,800,200), clrName, "box");
         //GUI.Label(new Rect(Screen.width/2, 15, 100, 20), "Hello World!");
         GUI.skin = null;
     }
 }
 
              
               Comment
              
 
               
              Color uses float values between 0 and 1, Color32 uses byte values between 0 and 255.
Your answer
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
Adding Item object to Inventory List 0 Answers
How to get a list of all the GUI elements in a scene? 1 Answer
Clamp/align horizontal view 2 Answers