randomly change between 6 colors C#

I want to change a material color between 6 different colors. I tried many things:

using UnityEngine;
using System.Collections;

public class changecolor : MonoBehaviour 
{

	void Star()
	{

		Color.cyan = 1;
		Color.red = 2;
		Color.green = 3;
		new Color(255, 165, 0) = 4;
		Color.yellow = 5;
		Color.magenta = 6;
	}
	void OnTriggerEnter(Collider other)
	{

		gameObject.renderer.material.color = Random.Range(1, 6);
	}
}

You want to use an array. Try this:

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)];
	}
}

How do i select color linearly from array ? means i apply this on multiple object so all object’s color will be changed together

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];*

  •  }*
    
  • }*
    Hope this may help you.
    NSKS

Could use a switch.

public int chosencolor;

void choosecolor()
{
chosencolor = random.range(0,5);

switch(chosencolor)
{
case 0:
//red
break;
case 1:
//green
break;
case 2:
//blue
break;
{
}