using UnityEngine;
using System.Collections;
public class changecolor : MonoBehaviour
{
Color[] colors = new Color[6];
void Start()
{
colors[0] = Color.cyan;
colors[1] = Color.red;
colors[2] = Color.green;
colors[3] = new Color(255, 165, 0);
colors[4] = Color.yellow;
colors[5] = Color.magenta;
}
void OnTriggerEnter(Collider other)
{
gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];
}
}
Note if you made colors ‘public’ you could get rid of the Start() and just pick the colors in the Inspector. Once this scripts is attached to the game object, set the size of the ‘colors’ array in the Inspector and then set each individual color.
using UnityEngine;
using System.Collections;
public class changecolor : MonoBehaviour
{
public Color[] colors;
void OnTriggerEnter(Collider other)
{
gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];
}
}
public GameObject ObjsDatChangeColor;//assign all Gameobjects to this
public Color ColorsArr;//assign all colors to this array
public int RequiredColorIndex;//index of the array in which desired color located
void OnTriggerEnter(Collider other)
{
for (int i = 0; i < ObjsDatChangeColor.Length; i++) {
ObjsDatChangeColor*.GetComponent().material.color = ColorsArr[RequiredColorIndex];*
public int chosencolor;
void choosecolor()
{
chosencolor = random.range(0,5);
switch(chosencolor)
{
case 0:
//red
break;
case 1:
//green
break;
case 2:
//blue
break;
{
}