Unity 5 Standard or Standard Specular - CHANGE Emission of material at run-time

In Unity 5, I have a material called “PlayerMaterial” and I’ve tried both the Standard and the Standard Specular to CHANGE the Emission level at run-time in C#.
You see, I can change it in the Inspector - BUT - during game play events occur where I want to INCREASE or DECREASE the Emission level (making the player appear brighter or dimmer) …

I’ve read over the DynamicGI and tried some things there but none have any effect on the Emission level.
See the screenshot of the Inspector - the yellow highlight is what I want to change at run-time in my C# code.2079145--135825--upload_2015-4-21_18-29-48.png

Ideas?

Thanks!2079145--135825--upload_2015-4-21_18-29-48.png

If you open up the Standard Shader, here are the variable names that you can use to access the Standard Shader properties:

_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo", 2D) = "white" {}
    
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

_Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
[Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
_MetallicGlossMap("Metallic", 2D) = "white" {}

_BumpScale("Scale", Float) = 1.0
_BumpMap("Normal Map", 2D) = "bump" {}

_Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
_ParallaxMap ("Height Map", 2D) = "black" {}

_OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
_OcclusionMap("Occlusion", 2D) = "white" {}

_EmissionColor("Color", Color) = (0,0,0)
_EmissionMap("Emission", 2D) = "white" {}
    
_DetailMask("Detail Mask", 2D) = "white" {}

_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
_DetailNormalMapScale("Scale", Float) = 1.0
_DetailNormalMap("Normal Map", 2D) = "bump" {}

[Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0

// UI-only data
[HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0
[HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1)

// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0

So, just call:

Renderer renderer = GetComponent<Renderer>();
Material material = renderer.material;
material.SetColor("_EmissionColor", ColorYouWant);

This Unity Answer explains more in-depth why you have to use _EmissionColor to change the value you want.

1 Like

Thanks a million … it worked!

1 Like

Hmm… So which one is Emission intensity, ie, the 3 in this image?

“Color finalValue=Color.White*someValue; // someValue adjust the scale of emission”

I just tested this, simply multiplying the colour by someValue will increase the emission value.

Source: Standard Shader Emission Value - Unity Engine - Unity Discussions

Hi I cannot get my head around this. There seems to be no direct way of controlling the ‘multiplier’ (yellow highlighted box in original post). My problem is that I am using a (cut and paste) script to up and down (animated pulse) the emission value during game play, this leaves the ‘multiplier’ at a random value when the game is stopped - which then needs resetting in the inspector. Is there a way to directly or indirectly reset the ‘multiplier’ value through the script? Here’s what I currently have as a script,

public class emissiveGlow : MonoBehaviour {

    public float frequency = 1f;
    public float amplitude = 1f;
    public Material material;
    public Color startEmissionColor = new Color(0.1F, 0.1F, 1.0F, 1F);
    public Color newEmissionColor;
    Renderer myRenderer;

    // Use this for initialization
    void Start()
    {
        myRenderer = GetComponent<Renderer>();
        newEmissionColor = startEmissionColor;
    }

    // Update is called once per frame
    void Update()
    {
        float glow = (2 + Mathf.Cos(Time.time * frequency)) * amplitude;
        material.SetColor("_EmissionColor", newEmissionColor * glow);
        DynamicGI.UpdateMaterials(myRenderer);
    }
}

Grab the original value before you start changing it and then have an OnDestroy function that resets it.

I don’t understand all the replies on this thread lol.
You can’t use math with a Color type?
Agrument2 cannot convert from mUnityEngine.Color to float.

                startPowerAmount += .005f;
                material.SetFloat("_EmissionColor",DefaultColor * startPowerAmount);