I'm trying to generate two random colors from a single array which are supposed not to be same. One of the colors is the bg color of the label, the other one is the text color of the text on top of it. But curiously i fail. Here is the 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;
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
};
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
};
// Use this for initialization
void Start () {
InvokeRepeating ("changeColors", 0, 1f);
}
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)];
}
}
// 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!");
}
}
Any spotted point of i've been missing? Thanks for your help in advance. Edit: I checked the indices to compare. They aren't same.