Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Changing a materials emission value on mouse over?

Discussion in 'Scripting' started by MSachs, Jan 11, 2018.

  1. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    117
    Hi,

    is it possible to change the emission value of a material on mouse over via script? I want to highlight an object when hovering over it with the mouse and I think doing so through raising the emission value could be a good option.

    Anybody has ideas on how to do that or has a different approach to highlighting an object?

    thanks in advance
     
  2. duisti

    duisti

    Joined:
    Nov 29, 2017
    Posts:
    52
    MSachs likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I find that adjusting the emission isn't enough. Either enable and disable it like so;
    Code (CSharp):
    1.     private Renderer _renderer;
    2.  
    3.     void Start ()
    4.     {
    5.         _renderer = GetComponent<Renderer>();  
    6.     }
    7.  
    8.     public void OnMouseEnter()
    9.     {
    10.         _renderer.material.EnableKeyword("_EMISSION");
    11.     }
    12.  
    13.     public void OnMouseExit()
    14.     {
    15.         _renderer.material.DisableKeyword("_EMISSION");
    16.     }
    or swap out the material.
     
    MSachs likes this.
  4. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    117
    Thanks guys. I will try it. :)
     
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    117
    I got it to work. Thanks guys :)
     
  6. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    That is frustrating that you need to use the enable/disable keyword. By just doing a SetColor("_EmissionColor"... I can see the emissive property change but not actually update in the viewport.