My script seems to work fine , but i seem to get this error when i play

IndexOutOfRangeException: Array index is out of range.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class DisplayColor : MonoBehaviour {
	 
	public Button[] buttons;






public	void Start () {

		changeColor ();

		}



public	void changeColor() {
		GetComponent<Image> ().color = buttons [Random.Range (0, buttons.Length - 1)].image.color;

	}
}

Okay, first off, please use more descriptive titles for your questions in the future, if you want good replies. Secondly, if your code is throwing that exception, then it is not working fine.

Third, this is a basic programming problem, not usually allowed on this site. You need to look up the IndexOutOfRangeException on MSDN and learn what it means. Then you need to add Debug.Log() calls to your code to output the actual values and compare that to what you expect. Once you’ve found the difference, you find where the code is going wrong to create that difference, and correct it to get the expected result. This procedure is called debugging.

Also, in the future, you’ll want to post screenshots of your hierarchy and scene view. Most likely, your problem is not defining the buttons array in Unity, but I have no idea if that’s the case, because you included no explanation in your question.

Your problem is that, when Buttons is an empty array, Random.Range (0, buttons.Length - 1) will return a value between 0 and -1, and you can’t access the -1ᵗʰ element of an array…

Since Random.Range is exclusive of the top integer value (docs), you should use Random.Range (0, buttons.Length), and also make sure that the buttons array actually has some elements in it.