Simple light script not working

Hello, I’m tying to create a simple script which will first light up red, then go back to black, then go to green etc.
I’ve created a simple code for this, I’ve tried to make it in C++ in Dev-C++, when running the (a little bit modified) code in Dev-C++ I’m getting the effect I’m looking for.
However for some reason the values RGB stay at 1 when running it in Unity. Even when i assign different values for either the R or G or B.
Here is my current code:

var R = 0;
var G = 0;
var B = 0;

function Update () {

    while(R<256){
    R++;
    light.color.r = R;
    }
    while(R>1){
    R--;
    light.color.r = R;
    }
    while(B<256){
    B++;
    light.color.b = B;
    }
    while(B>1){
    B--;
    light.color.b = B;
    }
    while(G<256){
    G++;
    light.color.g = G;
    }
    while(G>1){
    G--;
    light.color.g = G;
    }
}

Because colour values are from 0 to 1, not 0 to 255.

aaaah ok thx! :slight_smile:
however, now my unity freezes as soon as it starts, this is due a while loop which will never finish afaik. But it seems to me that my while loops are fine?

var R = 0;
var G = 0;
var B = 0;

function Update () {

    while(R<1){
    R+=0.01;
    light.color.r = R;
    }
    while(R>0){
    R-=0.01;
    light.color.r = R;
    }
    while(B<1){
    B+=0.01;
    light.color.b = B;
    }
    while(B>0){
    B-=0.01;
    light.color.b = B;
    }
    while(G<1){
    G+=0.01;
    light.color.g = G;
    }
    while(G>0){
    G-=0.01;
    light.color.g = G;
    }
}

NVM think i found the problem already, these are int’s instead of floats! :slight_smile:

var R : float = 0.00;
var G : float = 0.00;
var B : float = 0.00;

function Update () {

    while(R<1){
    R=R+0.001;
    light.color.r = R;
    }
    while(R>0){
    R=R-0.001;
    light.color.r = R;
    }
    while(B<1){
    B=B+0.001;
    light.color.b = B;
    }
    while(B>0){
    B=B-0.001;
    light.color.b = B;
    }
    while(G<1){
    G=G+0.001;
    light.color.g = G;
    }
    while(G>0){
    G=G-0.001;
    light.color.g = G;
    }
}

Now i get runtime errors Oo and its still not working! =/ any1 suggestions?
Edit, runtime error gone. However its still not working, these are the values unity gives:
R,G and B:
-0.0009906754
Temp:
9.324634e-06

any1? shouldnt be a hard solution if you ask me D:

it looks like you’re constantly both adding and subtracting 0.001. while > 0 you subtract, and while < 1 you add. It’s always greater than 0 and smaller than 1.

I dont think so, and in Devc++ its working perfectly fine.
im first adding 0.001 untill r = 1, then i will substract 0.001 until r = 0, then i will add 0.001 until b = 1 etc etc etc.

Keep in mind that the Update function gets executed every frame. This means that Unity is running through all of your while loops every frame. They are working, but you will only see the last result
You can use Coroutines instead and call yield in every while loop.

var R : float = 0.00;
var G : float = 0.00;
var B : float = 0.00;

function Start() {
   StartCoroutine(lightTransition());
}

function lightTransition(){
	while(R<1){
    	R=R+0.001;
    	light.color.r = R;
    	yield;
    }
    while(R>0){
    	R=R-0.001;
    	light.color.r = R;
    	yield;
    }
    while(B<1){
    	B=B+0.001;
    	light.color.b = B;
    	yield;
    }
    while(B>0){
    	B=B-0.001;
    	light.color.b = B;
    	yield;
    }
    while(G<1){
    	G=G+0.001;
   		light.color.g = G;
    	yield;
    }
    while(G>0){
		G=G-0.001;
    	light.color.g = G;
    	yield;
    }
}

That works perfectly fine VWekker! THX! :slight_smile:
Improved code:

var R : float = 0.00;

var G : float = 0.00;

var B : float = 0.00;

var Speed : float = 0.01;

function Start() {

   StartCoroutine(lightTransition());

}



function lightTransition(){

	while(true){

	while(R<1){

    	R=R+Speed;

    	light.color.r = R;

    	yield;

    }

    while(R>0){

    	R=R-Speed;

    	light.color.r = R;

    	yield;

    }

    while(B<1){

    	B=B+Speed;

    	light.color.b = B;

    	yield;

    }

    while(B>0){

    	B=B-Speed;

    	light.color.b = B;

    	yield;

    }

    while(G<1){

    	G=G+Speed;

   		light.color.g = G;

    	yield;

    }

    while(G>0){

		G=G-Speed;

    	light.color.g = G;

    	yield;

    }

   }

}