- Home /
Changing light colour makes it too bright
Hey there. So I'm trying to have different colour spells according to what button you press on the Razer Hydra. I have code that I think should work but obviously isn't and I'm not sure why. When the demo starts the light is way to bright. If I remove the line of code in the Start() method that sets the colour, it is fine and has the right range and intensity that I want. However if I add this code in or change the light colour by a button press, it gets messed up and too bright.
Here is a picture example with the code to see also.
Thanks for any help with this
Answer by TropicalTrevor · Mar 20, 2014 at 07:04 PM
This code works for me, if I mimick it in a small use case that is:
using UnityEngine;
public class tmp : MonoBehaviour
{
[SerializeField]
Color c = Color.white;
[SerializeField]
float i = 0.25f;
void Start()
{
//Light light = GetComponent<Light>();
light.color = c;
light.intensity = i;
light.range = 3;
}
}
I changed the color in the inspector to various things and it all turned out correctly.
Two things I would like to tell you
Light is multiplicative, if you have a light source that is 100% blue (so new Color(0,0,255);) and put it in a scene that has the default material, everything is blue. But if you create a new material and make the color red (255,0,0) everything will be black. This is because blue * red = black, red contains no blue so blue light only shows the blue part, which is zero. (0*255, 0*0, 255*0) = (0,0,0)
So in short: I think your light color mismatches with your material color making some objects appear correct and other over-bright.
Tips: Make your light look right when the light is white, desaturate your textures so colored lights can influence the world with whaterver color they have and possibly desaturate the lights either way. Colors look bright in comparison to their surrounding, if something is only a little red in a grey world it will still look very red so don't worry about your world becoming too flat - as long as the contrast is right.
Additionally (if you have pro) you can solve the boring-ness of colors by upgrading the saturation with a post effect.
Another, totally unrelated thing, I'd also like to recommend you to use a public or serialized field to put the light in like so (this is not related to your question at all but I just noticed it in your code):
public class MyClass : MonoBehaviour
{
[SerializeField]
Light spellLight;
/*
void Start() etcetera
*/
}
So you can use drag and drop in the editor instead of using gameobject.find -> which will give problems when you have more than 1 object with this name, possibly one having no light source at all etcetera. Also it is faster on startup and gives better error messages (null reference on the specific property so you know the object that was in there was deleted or has not been filled in etcetera).
@TropicalTrevor Thanks so much for that super in-depth response, really appreciate it and it helped me understand quite a bit! However, the code you provided didn't work for me :S Not sure what I'm doing wrong here but here is the code I have now.
I attached the light that this script is attached to in the inspector as you mentioned above.
Here is a picture of the IDE if you want to see the inspector: http://puu.sh/7CZOm.jpg
using UnityEngine;
using System.Collections;
public class ChangeSpellLight : $$anonymous$$onoBehaviour {
[SerializeField]
Color LightSpell = Color.white;
[SerializeField]
float intensity = 0.25f;
[SerializeField]
Light SpellLight;
void Start(){
SpellLight.color = LightSpell;
SpellLight.intensity = intensity;
SpellLight.range = 3;
}
Thanks!
EDIT
I fixed it but in a different way. When I used
SpellLight.color = LightSpell;
It didn't work and was super bright. However
SpellLight.color = Color.white;
Worked fine. Any ideas why this is?
that's an interesting problem, I don't know why this is however you could assign lightSpell in Awake or use a public property so you can edit the value in the inspector.
Are you sure you didn't get an error when assigning LightSpell and then the range and intensity were never set (so probably the range was at 12, the intensity at 1, the color at default, so the light is much brighter because it is much bigger etcetera)?
found it! stupid for me not to see this... colors range from 0.0f to 1.0f, anything beyond is stored and used so things become ridiculously bright! if you want to use sensible values create a const float COLOR_CONVERSION = 1f / 256f;
@TropicalTrevor Ah thanks for that. However I'm not understanding this one thing.
If I have this:
SpellLight.color = Color.white;
And it works, why does having
SpellLight.color = LightSpell;
Where LightSpell is defined at the top as this:
Color LightSpell = Color.white;
Not work? I understand what your saying about having to convert them but would setting them as a color not be the proper way?
Also for what you said about COLOR_CONVERSION, how would I do it for this as an example?
private Color FireSpell = new Color(225,113,12);
Thanks
I don't see why the first bit isn't working (Color LightSpell = Color.white should be fine).
But if you do like this:
class TestClass : $$anonymous$$onoBehaviour
{
const float COLOR_CONVERSION = 0.00390625f; //that is 1/256f;
Color fireSpell = new Color(225 * COLOR_CONVERSION,
113 * COLOR_CONVERSION, 12 * COLOR_CONVERSION);
}
Or maybe even this (not sure if it works though):
Color fireSpell = new Color(225,113,12) * COLOR_CONVERSION;
you should get the result you want while being able to copy the numbers from Unity or other software's color picker.
So I guess you should check if you don't redefine LightSpell somewhere or whether something else goes wrong there because I can't see anything wrong with the code itself.
Answer by Lord_Slimeball · Jan 31, 2021 at 11:31 AM
Hi, so the solution is here:
ambient light values have to be between 0 and 1, 23 for instance is not a valid value
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
If statement breaking button color copy 0 Answers
Distribute terrain in zones 3 Answers
UI Button color 1 Answer
Changing light Intensity though script does nothing 1 Answer