5

I'm having trouble with unity and here is the code

The error message:

IndexOutOfRangeException: Array index is out of range.
Sequence.fillSequenceArray () (at Assets/Scripts/Sequence.cs:43)
Sequence.Start () (at Assets/Scripts/Sequence.cs:23)

Code:

public int[] colorSequence = new int[100];
public int level = 2;

// Use this for initialization
void Start () {
    anim = GetComponent("Animator") as Animator;
    fillSequenceArray ();
    showArray (); // just to know

}

// Update is called once per frame
void Update () {

}

public void showArray(){
    for (int i = 0; i < colorSequence.Length; i++) {
        Debug.Log ("Position " + i + ":" + colorSequence[i]);
            }
}

 public void fillSequenceArray(){
    for (int i = 0; i < level; i++) {
        int numRandom = Random.Range (0, 3);    

        if (colorSequence[i] == 0) {
            colorSequence[i] = numRandom;
        }
    }
}

I have tried to change the last if to if (!colorSequence[i].Equals(null)), or if (colorSequence[i] == null) and the same error occurs. Even if I delete this if, the error occurs when I try to fill colorSequence[i] = numRandom;

3
  • Where is colorSequence initialized? Commented Nov 25, 2014 at 18:43
  • sorry, edited public int[] colorSequence = new int[100]; Commented Nov 25, 2014 at 18:56
  • Is is possible that you modified the 'colorSequence' variable in the editor? Values in the changed in the editor will override the ones initialized in the code. Commented Jul 15, 2016 at 18:19

2 Answers 2

2

You have to check whether the the array contains a value at that index before attempting to access it. Doing otherwise will throw an error instead of returning null.

You can easily check it using the array's length property:

    if (colorSequence.Length > i) {
        colorSequence[i] = numRandom;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

colorSequence.Length will return the size of array right ? The array was instantiated like this public int[] colorSequence = new int[100]; So will return 100 ?
I'm not getting why this is happening, I used to do this in java and all works .. I'm really in trouble with c#
So, I just want to know if the position of array is empty and fill the empty position with the random number
0

Unlike many languages, C# does not automatically fill an array "slot" outside of its bounds with null when you try to access it. When you first declare the array, you must explicitly state its length, and any attempt to read or write outside those bounds will generate an error.

var colorSequence = new int[length];

If you want a list that can dynamically grow or shrink as you add or remove items, you probably want List<T> instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.