- Home /
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;
 
     }
 }
 
Your script is fine as is. $$anonymous$$ost likely, your array is empty and random returns 0 where there is no item in the array.
Well fafase i have been facing this problem since i have tried calling changeColor from other script , i am attaching that script as well,
  using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class onClick1 : $$anonymous$$onoBehaviour {
 
     public Text ScoreText;
     private int Count = 10;
     private int wrongAnswer = -5;
     public bool hello;
 
     public    void SayHello(  bool hello = false ) {
 
         
       if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
 
             GameObject thePlayer = GameObject.Find("DisplayButton");
             DisplayColor displayColor = thePlayer.GetComponent<DisplayColor>();
         
 
             hello = true;
             displayColor.changeColor();
             ScoreText.text = "SCORE: " + Count;
 
         
         
         
         } else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
     
             hello = false;
             ScoreText.text = "SCORE: " + wrongAnswer;
         
         }
         }
     
 }
Answer by FortisVenaliter · Jun 12, 2015 at 06:38 PM
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.
well, this is no real answer to the question, more of a guideline. Better off as a comment.
well ty fafase , and fortis i would ask any question that i want , and if you want to answer then help or otherwise don't . Because thats not an answer its just a procedure
It's a procedure that's incredibly important to game development. Sorry if I came across as harsh; I certainly could have worded it better. But if you don't learn the process, you're going to end up with more frustration in the long run. It goes back to the whole "give a man a fish"/"$$anonymous$$ch him to fish" thing (although, admittedly, my answer is not $$anonymous$$ching). If you have the resources and skills to figure out the problems yourself, you'll find program$$anonymous$$g more rewarding.
Answer by tanoshimi · Jun 13, 2015 at 07:28 AM
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.
Thankyou guys for your help , I have figured my mistake , it was quite foolish i forgot to delete an empty gameObject .
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                