Change the emission value of the material by code

I have 4 objects with 1 different color materials for each one and I need to control the emission intensity value of the each material through of a script.

I am trying this:

public float intensity = 0.3f;

finalTargets [currentId].GetComponent<Renderer>().material.SetColor ("_EmissionColor", Color * intensity);

But I am getting some errors:

I read some solutions about to change the color but I need only to change the emission value. Is it possible? If so, please, tell me how can I do this.

Thanks in advance.

3122618--236411--Screen Shot 2017-06-26 at 15.04.22.png

As far as I know that value isn’t a separate variable to change. It’s akin to the “V” or “value” slider in the HSVA mode in the color picker.

After your answer I have verified the color picker and I figured out this is not a “intensity” field but a “brightness” field. Is it possible to control this value by code? Maybe by multiplying the color by a brightness factor. If so, how can I do to check the material color of each object before multiply it by the factor? And if the color is not accurate (e.g. not totally blue)?

I found a possible solution in this thread: Unity 5 Standard or Standard Specular - CHANGE Emission of material at run-time - Unity Engine - Unity Discussions

However, in this case I am changing the color but it’s not shining and when I change the brightness thought the inspector panel the color shines. Can I do it by code?

3123727--236523--screen.png

“Brightness” is just a multiplier as far as I know, try multiplying your color.

I did it: the color becomes more saturated but without the glow

Unfortunately I don’t know any more detailed information about how emissive materials work in Unity.

Check out this resource if you haven’t:

It’s down for maintenance right now though, so check back later.

Try using “Mathf.LinearToGammaSpace(intensity)” on your intensity and see how that affects the end result. If you’re seeing it work via the inspector, there’s definitely a way to do it through code.

I have tried “Mathf.LinearToGammaSpace(intensity)” before and the result also affected only the material color but not its brightness. I will check the tutorial about the emissive materials. Its very interesting and I hope it helps me.

Thanks for all your help so far.

1 Like

I am posting these two new screenshots just to clarify my needs.

3124055--236564--Screen Shot 2017-06-27 at 14.38.20.png
3124055--236565--Screen Shot 2017-06-27 at 14.38.37.png

Just to clarify, are you still getting the original errors from your first post? Would you share what your latest attempt in code was?

1 Like

No. I changed my first code totally.

I found a solution (Dynamic Emission in Real Time GI? - Unity Engine - Unity Discussions):

DynamicGI.SetEmissive(GetComponent<Renderer>(), yourcolor);

I changed “yourcolor” to Color.white and now I have a glow but it’s not colorful. As I don’t intend to use an accurate color (green/yellow/etc) I will use the same color that I defined in the emission color through the brightness value for each object.

Thanks

2 Likes

I really found this thread to have all the answers, and other useful info, clearly laid out. RECOMMENDED. :slight_smile:

2 Likes

Great Work Mate!

1 Like