GUI slider controls sunlight color?

Hello, I'd like to ask how do I change the color of a light (sunlight) via a slider?

Say, I have a specific set of colors that I want the slider to change the light's color to when it reaches a certain value.

Example: At value 0, the color is light blue; at value 50, the color is orange; at value 100, the color is purple.

I came up with this script, but it doesn't seem to work; the color doesn't really change. If someone could point me in the right direction on how to work around this?

var lights : Light[]; var Sunlight : Light; var SunColorSliderValue : float = 0.0; var SunColors : Color[]; var guiSkin : GUISkin; var toggleBoolSUN : boolean = true; private var toggleRect = Rect(15, 20, 135, 45);

function OnGUI ()

{ GUILayout.BeginArea( Rect( 10, 410, 240, 180 ),GUI.skin.window);

 toggleBoolSUN = GUI.Toggle(toggleRect, toggleBoolSUN, "Sunlight", guiSkin.button);

 lights[0].enabled = toggleBoolSUN;

         SunColorSliderValue = GUI.VerticalSlider(Rect(185,65,30,100), SunColorSliderValue, 0, 100);
 Sunlight.color = SunColorSliderValue;

 if(SunColorSliderValue == 0)
     Sunlight.color = SunColour[0];
 if(SunColorSliderValue == 50)
     Sunlight.color = SunColour[1];
 if(SunColorSliderValue == 100)
     Sunlight.color = SunColour[2];

GUILayout.EndArea(); }

Thanks for the help! =)

more ▼

asked Apr 13, 2011 at 06:42 AM

avatar image

157 17 18 31

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

If you want a smooth transition, look into the Color.Lerp function. http://unity3d.com/support/documentation/ScriptReference/Color.Lerp.html

 if (SunColorSliderValue < 50)
   Sunlight.color = Color.Lerp(SunColour[0], SunColour[1], SunColorSliderValue/50);
   Sunlight.color = Color.Lerp(SunColour[1], SunColour[2], (SunColorSliderValue-50)/50);

I have some VRML utilities that include a sorta multi-interpolator that allows as many different colors as you want, two lines of code, I should publish them.

more ▼

answered Apr 13, 2011 at 07:08 AM

avatar image

33.8k 419 348 547

Hmm ok, I just checked out the lerp function on the script reference, but according to what I see, it's only possible to change the color smoothly as time passes. I want to change the color smoothly through a slider. Perhaps you could show me how? =)

Apr 14, 2011 at 05:21 AM Nolirneen

You can feed any fraction into lerp, whether it's time or distance or whatever.

Apr 14, 2011 at 07:35 AM DaveA

Could you show me how, please? I'm new to programming and I've tried everything I could, and I just keep getting compiling errors.

Apr 15, 2011 at 06:14 AM Nolirneen

Added code, above

Apr 15, 2011 at 05:15 PM DaveA

Alright it works! I wanted the light to change between 4 colors, but I'll try to figure it out. Thanks again! =)

Apr 18, 2011 at 07:01 AM Nolirneen
(comments are locked)
10|3000 characters needed characters left

For the slider control, I am also not quite familiar. I previously studied something on the UI slider control, however, the C# tutorial for slider control make me feel so complicated. Don't know if that kind of thing be helpful to you. The proper answer here offered me much insights. I picked up the courage to continue digging into the GUI slider.

more ▼

answered May 09, 2014 at 04:41 AM

avatar image

Uchiha Itachi
0 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Apr 13, 2011 at 06:42 AM

Seen: 2544 times

Last Updated: May 09, 2014 at 04:41 AM