title: How to create a material picker option in unity?
body: <p>I have created a floor where by gazing on it the material changes for every 2 seconds. <strong>Now I wanted to create a material picker on the scene.</strong> <strong>Where the user can choose from the given 3 options of the materials and by clicking on it the selected material should apply to the floor. How to do it?</strong> </p>
  
  <p><strong>The source code for gazing floor to change material:-</strong></p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  
  using UnityEngine;
  
  //Make sure to change the class name (CCSphere) to whatever you called your 
  //script.
  
  public class tochangematerial : MonoBehaviour
  {
      public float gazeTime = 2f;
  
      private float timer;
  
      private bool gazedAt;
  
      public Material[] materials;//Allows input of material colors in a set size of array;
      public Renderer Rend; //What are we rendering? Input object(Sphere,Cylinder,...) to render.
  
      private int index = 1;//Initialize at 1, otherwise you have to press the ball twice to change colors at first.
  
      // Use this for initialization
      void Start()
      {
          Rend = GetComponent&lt;Renderer&gt;();//Gives functionality for the renderer
          Rend.enabled = true;//Makes the rendered 3d object visable if enabled;
      }
      void Update()
      {
          if (gazedAt)
          {
              timer += Time.deltaTime;
  
              if (timer &gt;= gazeTime)
              {
                  if (materials.Length == 0)//If there are no materials nothing happens.
                      return;
  
                  index += 1;//When mouse is pressed down we increment up to the next index location
  
                  if (index == materials.Length + 1)//When it reaches the end of the materials it starts over.
                      index = 1;
  
                  print(index);//used for debugging
  
                  Rend.sharedMaterial = materials[index - 1]; //This sets the material color values inside the index
  
                  timer = 0f;
              }
          }
      }
      public void pointerenter()
      {
          //Debug.Log("pointer enter");
          gazedAt = true;
      }
      public void pointerexit()
      {
          //Debug.Log("pointer exit");
          gazedAt = false;
      }
  }
  </code></pre>
  
  <p>Edited code:-</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  // Change renderer's material each changeInterval
  // seconds from the material array defined in the inspector.
  public class color2 : MonoBehaviour
  {
      public Material[] materials;
      public float changeInterval = 0.33F;
      public Renderer rend;
  
      void Start()
      {
          rend = GetComponent&lt;Renderer&gt;();
          rend.enabled = true;
      }
  
      public void Update()
      {
          if (materials.Length == 0)
              return;
  
          // we want this material index now
          int index = Mathf.FloorToInt(Time.time / changeInterval);
  
          // take a modulo with materials count so that animation repeats
          index = index % materials.Length;
  
          // assign it to the renderer
          rend.sharedMaterial = materials[index];
      }
  }
  </code></pre>
  
  <p>I used this code to change the color of a cube if i pressed a button. But it's not working. I have added this code to cube and in button I have attached this script and function. If I pressed the button the color of cube does not change. </p>
  
  <p><a href="https://i.stack.imgur.com/lcg7I.png" rel="nofollow noreferrer">I have added this script to cube and then in button I have dragged and dropped the gameobject(cube) and triggered the function void update() </a></p>
  
  <p>Edited again:-
  Now I can change the materials of the 3d model for eg:if it is a chair I am able to change the materials of chair by clicking on a button. How can I change the different models? eg:in chair there are different models if a user clicked on a button it should produce different models how to do it?</p>