Disable and enable emission property of a material via script?

Edit: Searched the web again and found a solution. For thos interested, here is a function containing the solution:

if (targetLight.enabled == true)
                {
                    GetComponent<Animation>().Play("Turn_On"); // Plays the light switch's on animation
                    emissionSourceMaterial.EnableKeyword("_EMISSION"); // sets the public variable emissionSourceMaterial's emission property active. Use DisableKeyword to disable emission.
                }

Hello, I am a bit new to scripting in Unity so sorry if this is a newbie question.

I have a script that toggles a point light when a light switch is clicked. I have the ceiling lamp that the light is supposed to look like it comes off of set with an emissive material, along with a non-emissive material for the lamp base. How do I reference the emission property of a specific material on a separate game object and set it to on or off? Thanks in advanced.

My code (Btw, sourceLamp variable holds the game object that has the material in which I wish to modify the emission property of):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightToggle : MonoBehaviour
{
    public Light targetLight;
    public GameObject sourceLamp;

    float distance;
    bool open;

    void Awake()
    {
        //player = GameObject.Find ("FPSController");  
        distance = 3;
    }

    void OnMouseOver()
    {
        Vector3 playerPosition = GameObject.Find("FPSController").transform.position;
        if (Input.GetMouseButtonDown(0))
        {        
            if (Vector3.Distance(playerPosition, transform.position) <= distance)
            {
                targetLight.enabled = !targetLight.enabled;
                Debug.Log(targetLight.enabled);

                if (targetLight.enabled == true)
                {
                    GetComponent<Animation>().Play("Turn_On");
                }
                else if (targetLight.enabled == false)
                {
                    GetComponent<Animation>().Play("Turn_Off");
                }
            }
        }
    }
}
4 Likes

Any ideas why DisableKeyword doesnt work for emission ?? Im trying
mat.DisableKeyword(“_EMISSION”); but no luck

Make sure your shader actually support the keyword. Maybe it’s named differently. Use inspector debug, or open sources of it.

1 Like

Thanks for the reply, I figured it out :slight_smile:

I needed:

mat.DisableKeyword("_EMISSION");
mat.globalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
mat.SetColor("_EmissionColor", Color.black);
10 Likes

My example (if someone needs).
This code turns on emission on hexes in player’s move range and turns it off for other hexes (enlisted in hexes[ ]):

foreach (GameObject x in hexes)
        {
            if (x != null)
            {
                if (Vector3.Distance(x.transform.position, transform.position) < plDist+0.5f)
                {
                    x.GetComponent<MeshRenderer>().material.EnableKeyword("_EMISSION");
                    x.GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", Color.yellow);
                }
                else
                {
                    x.GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", Color.black);
                }
            }
        }
4 Likes

what

what does hexes[ ] stand for here ?

1 Like