Hi! Guys I am working on a project and I am changing materials of a gameobject at run time. I have made an array and stored all materials in it. On every key pressed I change the material but it is giving me the error Array index out of range exception: on line 10,18 and 26. On every point where i am changing the matterial while I can not recognize the problem. Here is my code.
using UnityEngine;
using System.Collections;
public class Materialchange : MonoBehaviour {
public Material[] mymaterials;
public int indexcount=0;
public Renderer rend;
void Start () {
indexcount = 0;
rend = GetComponent<Renderer> ();
rend.material = mymaterials [indexcount];
}
void Update () {
if (Input.GetKeyDown (KeyCode.RightArrow) || Input.GetKeyDown (KeyCode.Return))
{
if (indexcount == 0 && indexcount < 10)
{
indexcount ++;
rend.material = mymaterials[indexcount];
}
}
else if (Input.GetKeyDown (KeyCode.LeftArrow) || Input.GetKeyDown (KeyCode.Backspace))
{
if (indexcount > 0 && indexcount < 10)
{
indexcount --;
rend.material = mymaterials[indexcount];
}
}
}
}
Help is appreciated. Sorry for bad english. Thanks in advance.