Problem with array

The following script is giving Array Out Of Range Exception`

void Start () {
	i = Random.Range (0, materials.Length-1);
	renderer.material = materials *;*
  • }*

  • // Update is called once per frame*

  • void Update () {*

  • }*
    `
    Please help

As somebody already pointed out, your code can only throw an exception index out of range if the array is empty.

Throw in a Debug line in Start, and it’ll show the length as 0:

void Start () {
    Debug.Log("materials length: " + materials.Length);
    i = Random.Range (0, materials.Length-1);
    renderer.material = materials *;*

}
If you’re getting the out of range exception, the output will be ‘materials length: 0’.