- Home /
How can i stop my update function after showing 5 random colors ?
i need my update function to slowly update my color ( 1 second - 1 color) using UnityEngine; using Unity.Engine.UI;
public class ColorChange : MonoBehaviour {
void Update() {
GetComponenet<Image>().color = new Color(Random.Range(0f, 1f),Random.Range(0f, 1f), Random.Range(0f, 1f));
}
}
Hey! Have a look at InvokeRepeating and CancelInvoke or coroutines. You can just have a variable storing the amount of times the color has been shown and once it reaches 5, stop changing it.
this is stoping after 5 seconds , but I also want it to only show 5 colors in 5 seconds not more than that -:
using UnityEngine; using System.Collections; using UnityEngine.UI; public class ColorChange : $$anonymous$$onoBehaviour {
float delay = 0;
void Update()
{
if (delay >= 5f) {
return;
} else{
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
delay += Time.deltaTime;
}
}
}
You can't stop the update function. It doesn't work that way. The Unity Engine will call the update function ever frame no matter what. Just use an accumulator and make that PART of the the update function test to false so it does not execute.
If I understood this correctly you want to change the color of an image each second for 5 seconds? In the same way as a traffic light, but with 5 colors and randomly?
using UnityEngine; using System.Collections; using UnityEngine.UI; public class ColorChange : $$anonymous$$onoBehaviour {
float delay = 0;
float timeLeft = 0;
void Update()
{
timeLeft+= Time.deltaTime;
if (delay >= 5f) {
return;
} else{
if (timeLeft >= 1f) {
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
timeLeft=0;
}
delay += Time.deltaTime;
}
}
Answer by Villaindro · May 05, 2015 at 01:05 AM
What about something like this:
// increase this to slow down color changed
float smooth = 0.05f;
int duration = 5;
// Use this for initialization
void Start()
{
StartCoroutine(FadeOut(5f));
}
// Update is called once per frame
void Update()
{
}
private IEnumerator FadeOut(float time)
{
float progress = 0;
float increment = smooth / time;
while (progress < 1)
{
Color colorToLerp = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
Color startColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
gameObject.GetComponent<Image>().color = Color.Lerp(startColor, colorToLerp, progress);
progress += increment;
yield return new WaitForSeconds(smooth);
}
}
Answer by Sondre-S · May 05, 2015 at 10:31 AM
Here's the code I use for my traffic light(you can just rename the colors to color1, color2 etc., add a forth and fifth color and set all colors to 1 sec). I don't know if you want to make sure it shows a different color each time, but if you do you'll have to store the colors already shown in some variables and make sure it doesnt show that color. Also this script is probably unneccesarily complicated, but it works:
var Seconds:float= 0.0169999f;
var Red:float= 15f;
var Yellow:float= 3f;
var Green:float = 15f;
var Green:float = 15f;
var TrafTimer:float;
var changeColors: boolean;
function Start() {}
function Update () {
if(changeColors){
TrafTimer += Seconds;
///// light is red ////////
if(TrafTimer > 0 && TrafTimer < Red)
{
// you can set a random color here
}
/////// light is yellow /////////
if(TrafTimer > Red && TrafTimer < (Red+Yellow))
{
// you can set a random color here
}
//////// light is green ////////
if(TrafTimer > (Red+Yellow) && TrafTimer < (Red+Yellow+Green))
{
// you can set a random color here
}
// light is yellow ///////////
if(TrafTimer > (Red+Yellow+Green))
{
// you can set a random color here
}
// Reset light back to red and/or just stop changing colors
if(TrafTimer >= Red+Yellow+Green+Yellow)
{
//TrafTimer=0;
changeColors= false; // don't run this anymore
}
}
}
thanks Sondre but it doesn't seem to work it only shows one color. but when I click on checkbox of changeColors then it changes color. please tell me what is wrong using UnityEngine; using System.Collections; using UnityEngine.UI; public class buttonColor : $$anonymous$$onoBehaviour {
//Here's the code I use for my traffic light(you can just rename the colors to color1, color2 etc., add a forth and fifth color and set all colors to 1 sec), also this script is probably unneccesary complicated, but it works:
public float Seconds = 1f;
public float color1 = 1f;
public float color2 = 1f;
public float color3 = 1f;
public float color4 = 1f;
public float color5 = 1f;
private float TrafTimer;
public bool changeColors ;
void Start() {}
void Update () {
if (changeColors) {
TrafTimer += Seconds;
///// light is red ////////
if (TrafTimer > 1 && TrafTimer < color1) {
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
}
/////// light is yellow /////////
if (TrafTimer > color1 && TrafTimer < (color1 + color2)) {
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
}
//////// light is green ////////
if (TrafTimer > (color1 + color2) && TrafTimer < (color1 + color2 + color3)) {
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
}
// light is yellow ///////////
if (TrafTimer > (color1 + color2 + color3 + color4)) {
GetComponent<Image> ().color = new Color (Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
}
// Reset
if (TrafTimer >= color1 + color2 + color3 + color4 + color5) {
TrafTimer = 0;
changeColors = false;
}
}
}
thanks "Ilgiz" . it is now showing 5 colors in 5 seconds . And if you could help me with one more thing - if there any way to make the all 5 colors of different shade. ( right now they are quite dull and same)
Guess you figured it out, but you have to set "change colors" to true in the declaration or in function Start().
you will need to Lerp between a few color (red -> yellow -> blue -> red) to do a rainbow transition.
try something like it
Color.Lerp(Color.red, Color.yellow, Random.Range (0f, 1f))
Color.Lerp(Color.yellow , Color.blue , Random.Range (0f, 1f))
Color.Lerp(Color.blue, Color.red, Random.Range (0f, 1f))