- Home /
C# change light color every few seconds
Hello,
I have an object and this object has a child(cubeLight). I need to change this light from the color blue to red every few seconds. so it changes to blue and back to red and back to blue etc. my code only changes it once and I have no idea why.
my code: public float resetTime = 0.5f; public Light cubeLight; void Awake() { StartCoroutine("GoBlue"); } IEnumerator GoBlue() { while(true) { cubeLight.color = Color.blue; Debug.Log("blue"); yield return new WaitForSeconds(resetTime); } } }
Answer by bodec · Jun 06, 2013 at 11:32 AM
in the awake function you ask it to change to blue you never change it to red or anything else from there out.
using UnityEngine; using System.Collections; public class Switch : $$anonymous$$onoBehaviour { public float resetTime = 0.5f; public Light cubeLight; void Awake() { StartCoroutine("GoBlue"); cubeLight.color = Color.blue; } IEnumerator GoBlue() { while(true) { yield return new WaitForSeconds(resetTime); } } }
won't work either :(
using UnityEngine;
using System.Collections;
public class lightchange : $$anonymous$$onoBehaviour {
public bool lightChange;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(lightChange){
light.color = Color.red;
}else{
light.color = Color.blue;
}
}
}
this will change the light based on a bool change short and simple just need to send a message from the trigger to the bool
if you want it to go non stop set up timers that change it back and forth and set a bool to turn on a function to start the timers ticking
what seems to work also is this
`IEnumerator GoBlue()
{
while(true)
{
cubeLight.color = Color.blue;
yield return new WaitForSeconds(resetTime);
cubeLight.color = Color.red;
yield return new WaitForSeconds(resetTime);
}
}`
it works good enough :P
Answer by fafase · Jun 06, 2013 at 02:15 PM
Two ways, either you use the update or you use a coroutine that never ends. The rest is just a timer.
In Update:
var timer:float;
Color[] color = new Color[2];
int index = 0;
function Start(){
color[0] = Color.blue;
color[1] = Color.red;
cubeCoor = color[index];
}
function Update(){
timer += Time.deltaTime;
if(timer > 2){
if(++index == color.Length)index = 0;
cubeColor = color[index];
timer = 0;
}
}
Coroutine:
function CoroutineChanger(){
var timer :float;
while(true){
while(timer < 2){
yield;
}
if(++index == color.Length)index = 0;
cubeColor = color[index];
timer = 0;
}
}
Same, same but different.
Answer by Emihaa · Aug 01, 2013 at 06:50 AM
Put this script to the object where the material is that you want to change color. Then put on the colorA and colorB the colors you want it to change. If you want to change the speed you can do something like "public float speed = 1.0F; " i guess. colorMaterial needs to be the name of the material you want to use. Also the "_Color" might be needed to change depending what the color is named on you shader.
using UnityEngine; using System.Collections;
public class ChangeColor : MonoBehaviour {
public Color colorA;
public Color colorB;
public float speed;
public Material colorMaterial;
void Update () {
Color color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time * speed, 1));
colorMaterial.SetColor("_Color", color);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613103629im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it possible to change keyboard input inGame 1 Answer