Changing Standard Shader Emissive Values At Runtime

I previously had my objects simply turning off (exampleObject.active = false) when turning the objects state to on or off, but now that it is all working, I am looking for something a little more graphically appealing, and acceptable for a game.
I changed the line to this (exampleObject.GetComponent().material.SetFloat(“_EmissionScaleUI”, 0.0f):wink: in order to turn the emission value up or down (to turn a series of lights on or off), but it doesn’t work; no errors but it doesn’t do anything.

To clarify, I am trying to change the Emission “brightness” value expressed as a float on the right of the highlighted section in the image below.
Ref: Unity - Manual: Emission

My full code (entire script) has been posted below, in the hopes someone may be able to spot something wrong I, and the Unity debugger cannot.

It may be a simple syntax issue, or the parameter name may have changed, but I have revieved an error each time I attempt to open the Standard Shader code, so I cannot see for myself.

using UnityEngine;
using System.Collections;

public class Gate : MonoBehaviour
{

    public GameObject[] lights;
    public Switch[] switches;
    private int lightCount;

    public int switchCount;
    private bool[] switchState;//holds previous state to compare to new/current state
    private bool changed;//a state has changed
    private int index = 0;

    void Awake()
    {
        switchState = new bool[switchCount];
    }

    void Start()
    {
        for (index = 0; index < lights.Length; index++)
        {
            //lights[index].active = false;
            lights [index].GetComponent<Renderer> ().material.SetFloat ("_EmissionScaleUI", 0.0f);

        }
    }

    void Update()
    {
        //check to see if any switch state has changed
        changed = false;
        for (index = 0; index < switchCount; index++)
        {
            if (switches[index].state != switchState[index])
            {
                changed = true;
                switchState[index] = switches[index].state;
                break;
            }
        }

        //if a state has changed 'index' will hold which one
        if (changed)
        {
            for (int lightCounter = 0; lightCounter < lights.Length; lightCounter++)
            {
                if (switches[index].lights[lightCounter] == true)//set in switches therefore act on this item
                {
                    bool lightIsActive = false;
                    //lights[lightCounter].active = !lights[lightCounter].active;
                    lightIsActive = !lightIsActive;
                    //lights[lightCounter].GetComponent<Renderer>().material.SetFloat("_EmissionScaleUI",2.0f) = lights[lightCounter].GetComponent<Renderer>().material.SetFloat("_EmissionScaleUI",0.0f);

                    if (lightIsActive) {
                        lights [lightCounter].GetComponent<Renderer> ().material.SetFloat ("_EmissionScaleUI", 2.0f);
                    }
                    else if (!lightIsActive)
                    {
                        lights [lightCounter].GetComponent<Renderer> ().material.SetFloat ("_EmissionScaleUI", 0.0f);
                    }
                }
            }

        }
    }
}

I never used the emissionScaleUI. Instead I just set the color to black for no emiision.

material.SetColor( "_EmissionColor",  Color.black);

and in your case set it to 2 * white for your emission

material.SetColor( "_EmissionColor",  2f * Color.white);
1 Like

Unfortunately, I am using lights[lightCounter].active = !lights[lightCounter].active; in my code, which seems to refuse anything but those values, so booleans, and anything else doesn’t work.

I think I may need a needlessly complicated solution for this…:frowning:

If people are coming here looking for how to change Emission values at runtime in code, I think this thread has all the best answers clearly laid out. :slight_smile: