This question was
closed Mar 03, 2017 at 02:41 PM by
PresidentPorpoise for the following reason:
Other
Question by
PresidentPorpoise · Dec 08, 2016 at 03:08 AM ·
scripting problemgameobjectscripting beginnerscriptingbasics
Disable and enable emission property of a material via C# script?
Edit: Fixed it with the help of some more google-ing. Here is an if statement 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");
}
}
}
}
}
Comment