Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 03, 2017 at 02:41 PM by PresidentPorpoise for the following reason:

Other

avatar image
4
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing script from another object 1 Answer

how to change the value of my slider(in the inspector) thanks 0 Answers

How can i run a script with a single key press? 1 Answer

Make enemy patrol without Nav Points? 0 Answers

When I change a bool in a script on one object it doesn't change the bool on the same script on another object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges