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

Self-Illuminate on MouseDown

Discussion in 'Scripting' started by hp5lamps, Jan 20, 2014.

  1. hp5lamps

    hp5lamps

    Joined:
    Jul 9, 2012
    Posts:
    13
    I have a button in my scene which I want to be able to set to light on and off with a mouse click.

    However, I have no idea whether it would be possible to change the object shader type from Diffuse to Self-Illumin at run time.

    Any ideas if this is possible?


    Thanks,

    hp
     
  2. TheShane

    TheShane

    Joined:
    May 26, 2013
    Posts:
    136
    Create a separate material and swap it in at run-time. It should be no problem at all.
     
  3. hp5lamps

    hp5lamps

    Joined:
    Jul 9, 2012
    Posts:
    13
    Solved this and should be fairly simple. Here is my code in case someone has the same issue.

    Code (csharp):
    1.  
    2. private var buttonOn = false;
    3.  
    4. var material : Material;
    5. var shader : Shader;
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13. }
    14.  
    15. function OnMouseDown() {
    16.     if(!buttonOn) {
    17.         buttonOn = true;
    18.                 renderer.material.shader = shader; 
    19.     } else {
    20.         buttonOn = false;
    21.          renderer.material = material;
    22.     }
    23. }
    24.  
    You have to drag the default shader in the inspector for the Material. The Shader can be selected in the inspector. I chose Self-Illum/Specular.