- Home /
using rgb to change color c#
I am trying to change an objects color through 3 sliders that are red, blue and green. i have a code setup where there is 3 Gui sliders that effect floats:
rSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), rSliderValue, 0.0F, 255.0F);
bSliderValue = GUI.HorizontalSlider(new Rect(25, 50, 100, 30), bSliderValue, 0.0F, 255.0F);
gSliderValue = GUI.HorizontalSlider(new Rect(25, 75, 100, 30), gSliderValue, 0.0F, 255.0F);
then lower down in my script i have some code that changes the color like this:
renderer.material.color = Color32(rSliderValue, bSliderValue, gSliderValue, 1);
im getting an error saying: Assets/Color_Change.cs(37,67): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
can anyone explain why this error is occurring as i cannot make sense of what the error is meant to tell me.
-Thanks, Harry
You don't write "solved" in the title to accept answers. Please watch the tutorial video (link on the right) if you don't know how to use this site.
Answer by dorpeleg · Oct 10, 2013 at 07:00 AM
Add 'new' before color:
renderer.material.color = new Color32(rSliderValue, bSliderValue, gSliderValue, 1);
Because you did not say 'new', the compiler looks at Color32 as the type instead of a value of a type.
So you where trying to make the type Color32 equal to something.
Where you actually need a variable of type Color32 to equal something.
I hope that made it clear.
hey, thanks for your response, but i am now getting another error using that code:
Assets/Color_Change.cs(37,123): error CS1502: The best overloaded method match for
UnityEngine.Color32.Color32(byte, > byte, byte, byte)' has some invalid > arguments and this one too: > Assets/Color_Change.cs(37,123): error > CS1503: Argument
#1' cannot convertfloat' expression to type
byte'
is this because i am using floats not bytes? and how would i change a float to a "byte" or ins$$anonymous$$d change the slider to output a byte?
-Harry
Is there a reason you are using Color32 ins$$anonymous$$d of Color? Color accepts floats between 0 and 1.