- Home /
change randomly colors
hello!!! I am trying to make a game... but i have problem... I want my object every 3 sec to change color randomly... 25% red 25% yellow 25% blue 25% green Pls can you help me on that?
Probably use a self repeating co-routine
e.g
IEnumerator ChangeColor()
{
//Your colour changing code
yield return new WaitForSeconds(3);
StartCoroutine("ChangeColor");
}
i was able to change random color at every 3 seconds using coroutine i hope any one who want to do similar thing will follow this question
public class RandomColorRotine : MonoBehaviour { private bool randomcolor;
[SerializeField]
private GameObject _cube;
// Start is called before the first frame update
void Update()
{
if (randomcolor == false)
{
StartCoroutine("InvisibleCube");
}
}
IEnumerator InvisibleCube()
{
randomcolor = true;
_cube.GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value);
yield return new WaitForSeconds(3f);
randomcolor = false;
}
}
im sorry i know this is a forum but......clor :insert mememan:.......big sorry couldn't stop myself
Answer by romatallinn · Nov 22, 2016 at 12:12 PM
For random colours you can use this:
1) https://docs.unity3d.com/ScriptReference/Random.ColorHSV.html
2) Color color = new Color(Random.Range(0f, 1f),Random.Range(0f, 1f),Random.Range(0f, 1f), 1); // Color
For timeline use this:
IEnumerator ColorCoroutine()
{
while(true)
{
// Color change
yield return new WaitForSeconds(3);
}
}
And in Start function, or wherever you want, put StartCoroutine("ColorCoroutine");
Your answer
Follow this Question
Related Questions
Simple Maths Problem (I Suck At Maths) 2 Answers
Calculating percentage in code is reversed? 1 Answer
How do I Adjust The Colour Of My Object By Percentages? 1 Answer
How to find % along path between N points 2 Answers
Simple math? 2 Answers