Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    122
    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:
    122
    Thanks guys. I will try it. :)
     
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    122
    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.