- Home /
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);
}
}
Answer by robertbu · Jan 15, 2014 at 01:30 AM
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)];
}
}
after some time I got the same code by my self but still had an error, it was because I was saying gameObject.renderer.material.color = color[Random.Range(0, colors.Length)];
ins$$anonymous$$d of gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];
Error CS1061 'Component' does not contain a definition for 'material' and no accessible extension method 'material' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?)
Error CS0619 'GameObject.renderer' is obsolete: 'Property renderer has been deprecated. Use GetComponent() ins$$anonymous$$d. (UnityUpgradable)'
i get these two errors any help??
void OnTriggerEnter(Collider other)
{
GetComponent<Renderer>().material.color = colors[Random.Range(0, colors.Length)];
}
Answer by knobblez · Dec 01, 2018 at 09:25 PM
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;
{
}
Answer by Vrushank26 · Dec 08, 2015 at 03:56 AM
How do i select color linearly from array ? means i apply this on multiple object so all object's color will be changed together
Answer by sandeepsmartest · Dec 08, 2015 at 04:59 AM
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[i].GetComponent<Renderer>().material.color = ColorsArr[RequiredColorIndex];
}
}
Hope this may help you. NSKS
Look, there are two cube prefab(both Prefab has same material) which are called by script in game, Now i have to change their color with respect to t time. and i have to add 5-6 color in this. For Ex:- every 10 s both cube prefab change their color from white to red after that 10s both change their color from red to yellow and so on. So now tell how to make script for it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613130443im_/https://answers.unity.com/themes/thub/images/avi.jpg)