title: Change Material of a Sphere on Focus for VR 360 Degree Image App in Unity3d
body: <p>I am creating an app for google-cardboard, App like 360 degree image.
  I created a sphere and I made 62 material with different 360 degree image.
  I created 2 button, next and previous, when i focus on next button its changing to next material as i want. but when i focus on previous button, its changing to last material which i dont want. What I want is just previous material.</p>
  
  <p>Here is my code. See and tell me what I am doing wrong. Thanks.</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class next : MonoBehaviour {
  
      public CardboardHead head;
      public Material[] myMaterials = new Material[60];
      int maxMaterials;
      int arrayPos = 0;
      public GameObject Sphere;
      public int timeToCompleteLoading = 3;
      bool wait = false;
  
      void Start ()
      {
          head = Camera.main.GetComponent&lt;StereoController&gt; ().Head;
          maxMaterials = myMaterials.Length-1;
  
      }
  
      public void ToggleVRMode() 
      {
          Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
      }
  
      IEnumerator RadialProgress(float time)
      {
          wait = true;
          yield return new WaitForSeconds(time);
  
          float rate = 1 / time;
          float i = 0;
          while (i &lt; 1)
          {
              i += Time.deltaTime * rate;
              gameObject.renderer.material.SetFloat("_Progress", i);
  
          }
          wait = false;
      }
  
      void updateMaterials(){
          if (collider.gameObject.tag == "next") {
              if (arrayPos == maxMaterials){
                  arrayPos = 0;
              }else{
  
                  arrayPos++;
                  Sphere.renderer.material = myMaterials [arrayPos];
              }
          }
           if (collider.gameObject.tag == "previous") {
              if (arrayPos == 0){
                  arrayPos = maxMaterials;
              }else{
                  arrayPos--;
                  Sphere.renderer.material = myMaterials [arrayPos];
  
              }
          }
  
  
      }
  
  
      void Update ()
      {
          RaycastHit hit;
          Collider collider = GetComponent&lt;Collider&gt; ();
  
          if (collider) {
  
              bool isLookAt = collider.Raycast (head.Gaze, out hit, Mathf.Infinity);
              if (isLookAt == true) {
                  if(wait == false){
                      StartCoroutine (RadialProgress (timeToCompleteLoading));
  
                      updateMaterials();
                  }
              }
          }
      }
  }
  </code></pre>