how to randomly choose a value from array
Evening.
I need to create an array - a bunch of colors hexes, and randomly choose one from the array. Ive tried to google - but all i've found is from 2013 tops, and not seem to work anymore.
At this point - how to create an array nor how to roll from it - is complete mystery. Please help.
Here what i got so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Color$$anonymous$$ap : $$anonymous$$onoBehaviour {
public string ColorString1;
public string ColorString2;
public string ColorString3;
public string ColorString4;
public string ColorString5;
public string ColorString6;
public Color Color1;
public Color Color2;
public Color Color3;
public Color Color4;
public Color Color5;
public Color Color6;
public void Start()
{
ColorString1 = "#e8f5e9";
ColorString2 = "#00c853";
ColorString3 = "#69f0ae";
ColorString4 = "#ffffff";
ColorString5 = "#00e676";
ColorString6 = "#b2ff59";
ColorUtility.TryParseHtmlString(ColorString1, out Color1);
ColorUtility.TryParseHtmlString(ColorString2, out Color2);
ColorUtility.TryParseHtmlString(ColorString3, out Color3);
ColorUtility.TryParseHtmlString(ColorString4, out Color4);
ColorUtility.TryParseHtmlString(ColorString5, out Color5);
ColorUtility.TryParseHtmlString(ColorString6, out Color6);
public Color[] ColorArrayBlue = new Color[] { Color1, Color2, Color3, Color4, Color5, Color6 };
}
}
It does not work. And the errors are not understandable at all =(
Answer by Vivaldin · Jul 08, 2017 at 04:27 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorMap : MonoBehaviour {
public Color[] ColorArrayBlue;
public string ColorString0;
public string ColorString1;
public string ColorString2;
public string ColorString3;
public string ColorString4;
public string ColorString5;
public Color Color0;
public Color Color1;
public Color Color2;
public Color Color3;
public Color Color4;
public Color Color5;
public void Start()
{
ColorString0 = "#e8f5e9";
ColorString1 = "#00c853";
ColorString2 = "#69f0ae";
ColorString3 = "#ffffff";
ColorString4 = "#00e676";
ColorString5 = "#b2ff59";
ColorUtility.TryParseHtmlString(ColorString0, out Color0);
ColorUtility.TryParseHtmlString(ColorString1, out Color1);
ColorUtility.TryParseHtmlString(ColorString2, out Color2);
ColorUtility.TryParseHtmlString(ColorString3, out Color3);
ColorUtility.TryParseHtmlString(ColorString4, out Color4);
ColorUtility.TryParseHtmlString(ColorString5, out Color5);
ColorArrayBlue = new Color[6] { Color0, Color1, Color2, Color3, Color4, Color5 };
}
}
Answer by Kishotta · Jun 25, 2017 at 11:48 PM
Without knowing how you are storing your hexes, I can't be more specific than this. But this should be pretty straightforward.
// Create the array, assuming your hex objects are of type "Hex"
Hex[] hexes = new Hex[hexCount];
// Populate the array
for (int i = 0; i < hexCount; i++) {
hexes[i] = /*however you reference your hexes*/;
}
// Get a random hex from the list by getting a random index number in the array range
Hex RandomHex = hexes[Random.Range (0, hexCount)];
Hex RandomHex = hexes[Random.Range (0, hexCount - 1)]; // hexCount-1 because Range is inclusive
With floats it is inclusive, with ints it is not. So you shouldn't subtract 1 here.
@Nose$$anonymous$$ills is correct. I've updated my answer.
Ive tried this one but the system tell me it cant understand the "Hex[]" I have to use some specific library for this?
Answer by Vivaldin · Jun 27, 2017 at 03:52 PM
Can i define the array somehow clearly?
is there something like this? ColorList = new Array (1111, 2222, 3333, 4444, 5555...) ?
You can with:
Color[] ColorArray= new Color[] {
Color.white,
Color.blue,
new Color (0.5f, 3.1415926f, $$anonymous$$athf.Sqrt(2), 0.1f),
Color.red
}
This is not working. I need to use hex codes of the colors. If i use color[] - i have to use RGB colors i guess. I have no idea of how to transform the hex in to RGB by code...