0

I'm getting an index our of range exception at the beginning of my program, but can't seem to figure out what's causing it. The array in question is of colors from where to pick from, and the index is 0, trying to select the very first color (blue).

The error happens on the awake stage, when Unity attempts to pick the first color by default.

Color array to prove it's not empty

Error and debug log proving it's actually picking the right color

    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    public class HexMapEditor : MonoBehaviour {

        public Color[] colors;

        public HexGrid hexGrid;

        private Color activeColor;

        void Awake() {
            SelectColor(0);

        }

        public void SelectColor (int index) {
            activeColor = colors[index];
            Debug.Log(activeColor);
        }

A different question here said the problem might be fixed by resetting the component, which I did but my problem wasn't fixed.

I'm really stumped here, so any help is appreciated.

  • What happens if you log `colors.Length`? Presumably you have more than one of this control and one has no entries in the colors array – Blam Feb 07 '17 at 22:24
  • Capital L in `Length` – Blam Feb 07 '17 at 22:26
  • It shows that there are 10 objects in the array. – Pedro Llorens Feb 07 '17 at 22:28
  • In your second screenshot the fact there is an error *after* the Debug.Log line proves that SelectColor is being called more than once and it's failing on the second attempt, meaning there could be more than one HexMapEditor object or the colors array is being replaced with an empty array somewhere. – Blam Feb 07 '17 at 22:30
  • Given the code shown, if the array really did have a non-zero length, and your index really was set to the value 0, then the exception would not be happening. So, either the exception is in some code you didn't show, or one of those other conditions is not true. See the marked duplicate for extensive advice on diagnosing and fixing this class of exceptions. If you after reviewing carefully and following the advice you're still stuck, post a new question with a good [mcve] that reliably reproduces the problem, and explain what specifically you've done to debug it, and what you need help with. – Peter Duniho Feb 07 '17 at 22:30
  • The error is raised in line 37 which means that you left out some code in the above. Please add that since the above code is working correct. – Gunnar B. Feb 07 '17 at 22:32
  • Both of your comments helped a lot, so thank you! Indeed the Colors array is being replaced with a 0 Length array on the second attempt at selecting a color (I don't even know why it's doing a second attempt). I just need to find where it's getting reset to a 0-length array now. Apparently it only happens once, because calling SelectColor in the future works without any trouble, but indeed always happens twice. – Pedro Llorens Feb 07 '17 at 22:40
  • Indeed I had a second object with the script attached. I appreciate all your help, the problem is fixed now! – Pedro Llorens Feb 07 '17 at 22:46

0 Answers0