For each color in array, reference in another method to set corresponding color with 50% alpha
I have a method which sets a range of base colors in an array. I then want to set a range of corresponding colors, with 50% alpha, via another method. I realise I can manually set the alpha against each base color, but because my base color array may grow to numerous colors, I thought it would save hassles in future to get the 50% alpha colors in a method. rather than me typing them all in.
public Color[] colorBase = new Color[6];
public Color[] colorWithAlpha = new Color[6];
void Start() {
SetBaseColors();
SetColorsWithAlpha();
}
void SetBaseColors() {
colorBase[0] = Color.cyan;
colorBase[1] = Color.red;
colorBase[2] = Color.green;
colorBase[3] = Color.blue;
colorBase[4] = Color.yellow;
colorBase[5] = Color.magenta;
}
// the below doesn't work:
void SetColorsWithAlpha() {
Color theColor;
for (int i = 0; i < colorBase.Length; i++) {
theColor = colorBase[i].maxColorComponent;
colorWithAlpha[i].a = theColor * 0.5f;
}
Comment
Answer by Catsy99 · Aug 17, 2016 at 11:40 AM
Hi, I ended up with this method, which works. If anyone has a more efficient way to do this, let me know:
void SetColorsWithAlpha() {
for (int i = 0; i < colorBase.Length; i++) {
colorWithAlpha[i] = colorBase[i];
colorWithAlpha[i].a *= 0.5f;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612090928im_/https://answers.unity.com/themes/thub/images/avi.jpg)