-1

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.

gokturk
  • 116
  • 2
  • 13
  • possible duplicate of [Unique (non-repeating) random numbers in O(1)?](http://stackoverflow.com/questions/196017/unique-non-repeating-random-numbers-in-o1) – rutter Jun 01 '15 at 22:28
  • It's not. I checked the indices. They aren't same. – gokturk Jun 02 '15 at 06:48

3 Answers3

1

I have a slightly less efficient way for you to do this. First clone your array of colors to a temporary list that you can change however you want. Then remove the index of the first random color. Here is an example:

void changeColors()
{
    List<Color> tempColors = new List<Color>(colorList);

    // Get the first random index.
    int a = (int)Random.Range (0, tempColors.Length);  
    // 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.Length); 
    // Assign second colour.
    txtClr = tempColors[b];   
}

I hope this can help you.

EDIT: If the problem turns out to be something else I think a code cleanup is appropriate here. Just to show you a different way of writing:

void changeColors(){
    int a,b;
    a = b = 0;

    while (a == b) 
    {
        a = (int)Random.Range (0, colorList.Length);
        b = (int)Random.Range (0, colorList.Length);
    }

    clr = colorList [a];
    txtClr = colorList [b];
    clrName = colorNames [Random.Range (0, colorNames.Length)];
}

This should do exactly the same as your current code if I'm not mistaken.

Reasurria
  • 1,808
  • 16
  • 14
  • Good point. I'll give it a try when i'm available. Thank you. – gokturk Jun 02 '15 at 08:02
  • Yeah you're right. I tried exactly the same code as in your edit but something else must be wrong because it didn't work as expected. Anyway, i'll try your first code then inform you. – gokturk Jun 02 '15 at 08:48
  • I checked the code but the issue still remains. As by far i could realize, only colors red and white causing problem. – gokturk Jun 02 '15 at 09:58
  • What weird behaviour. Maybe add some code to call your function on a key press to eliminate timing issues. – Reasurria Jun 02 '15 at 10:34
  • What i'm mostly suspicious of is a timing issue. But i can't come up with a proper solution about it. – gokturk Jun 02 '15 at 10:37
  • What i recently realized is even though i commented out the white rgb color code in the list colorList, it kept setting backgroun color to white. Any ideas why so? – gokturk Jun 02 '15 at 13:54
  • I wonder.. I'm not sure but what if you put the values from 0 - 1 instead of 0 - 255? – Reasurria Jun 03 '15 at 07:30
0

Why did you putted it in invokerepeating if you only need two colors only one time and as a side note for invokerepeating start time don't put zero if you want it to start right away put a small value in start time

 void Start () {
    Changecolors();
}
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
0

"Color uses float values between 0 and 1, Color32 uses byte values between 0 and 255.

http://docs.unity3d.com/ScriptReference/Color-ctor.html

http://docs.unity3d.com/ScriptReference/Color32.html

Answer

alucardj solved it.

gokturk
  • 116
  • 2
  • 13