title: Scripting GUI buttons to trigger an event of changing materials
body: <p>Apologies if there's a similar question, however, I have probably seen it and it has not fixed my problem.</p>
  
  <p>I am trying to write a JS script for unity in order to achieve an event to be triggered once clicked. </p>
  
  <p>I have searched online on UnityAnswers website and others, the closest I can get is based on these questions 
  <a href="http://answers.unity3d.com/questions/368303/changing-shaders-of-a-gameobject-via-script-1.html" rel="nofollow noreferrer">http://answers.unity3d.com/questions/368303/changing-shaders-of-a-gameobject-via-script-1.html</a></p>
  
  <p>and this one
  <a href="http://answers.unity3d.com/questions/319875/change-objects-material-using-gui-buttons-via-scri.html" rel="nofollow noreferrer">http://answers.unity3d.com/questions/319875/change-objects-material-using-gui-buttons-via-scri.html</a></p>
  
  <p>and also looked at this one 
  <a href="http://docs.unity3d.com/ScriptReference/Material-shader.html" rel="nofollow noreferrer">http://docs.unity3d.com/ScriptReference/Material-shader.html</a></p>
  
  <p>So, my code is this so far</p>
  
  <pre><code>var button2_tex : Texture;
  var button3_tex : Texture;
  var button4_tex : Texture;
  var seat_mat1 : Material;
  var seat_mat2 : Material;
  var veneer1 : Texture;
  var veneer2 : Texture;
  var rend : Renderer;
  var _mouseDown = false;
  
  
  
  function Start() {
  
      seat_mat1 = Resources.Load( "seat_mat1" );
      seat_mat2 = Resources.Load( "seat_mat2" );
  
  }
  
  function Update(){
  
          if(Input.GetMouseButtonDown(0)){
  
              _mouseDown = true;
  
          }
  
  }
  
  function OnGUI() {
  
  GUI.Box (Rect (10,10,100,200), "Menu");
  
          if (_mouseDown){
  
              if (GUI.Button (Rect (20,40,40,20), button1_tex)){
  
              if(seat_mat1){
  
  
                  rend.material = seat_mat2;
                  Debug.Log("This button was clicked!");
                  }
  
              else{
  
                  rend.material = seat_mat1;
                  }
  
              }
  
          }
  </code></pre>
  
  <p>Please note some variables I haven't used yet, as am still testing bunch of other codes to get it working..</p>
  
  <p>the code snippet I am trying to fix starts with "function OnGUI()" but I maybe wrong and could use some fresh insight. </p>
  
  <p>this is a screenshot of the resulting script. The button on the left side is supposedly to change the colour of the material from seat_mat1 to seat_mat2 by the event of mouse clicking on the button. </p>
  
  <p><a href="https://i.stack.imgur.com/rzg4g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rzg4g.png" alt="enter image description here"></a></p>
  
  <p>I have attached the previous script to the 3D object in unity and had made a folder names "Resources" for the materials to be visible and referenced through the script.</p>
  
  <p>My problem is that upon clicking the GUI button, nothing happens, and it maybe something very simple and I am just missing it .. apologies for being inexperienced in JS much or unity. </p>
  
  <p>Thanks in advance. </p>
  
  <p>EDIT: </p>
  
  <p>So after playing a bit more with the code. I added a Debug.Log() after this line </p>
  
  <pre><code>GetComponent.&lt;Renderer&gt;().material = seat_mat2;
  Debug.Log("This button was clicked!");
  </code></pre>
  
  <p>and seems to be this error that I am getting every time the button is pressed</p>
  
  <blockquote>
    <p>"MissingComponentException: There is no 'Renderer' attached to the "scene_export3" game object, but a script is trying to access it.
        You probably need to add a Renderer to the game object "scene_export3". Or your script needs to check if the component is attached before using it.
        materialChanger.OnGUI () (at Assets/materialChanger.js:44)"</p>
  </blockquote>
  
  <p>So with simple understanding, it seems that the renderer is not attached somehow?</p>