Argument out of range

In my really simple simon says game, everything used to be working fine. The game would generate a pattern, and when the player got the pattern correct, the computer would give the same thing but add another color. However, when I went to add a high score system, basically everything broke. On the second round of a pattern, the first color would not repeat, and I get an error that says ‘Argument out of range’. Its probably something really easy to fix, I don’t see anything wrong with it, but if anyone else does, I would really appreciate it. My script:

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

public class GameManager : MonoBehaviour {

	public SpriteRenderer[] colors;

	public AudioSource[] buttonSounds;

	private int colorSelect;

	public float lit;

	private float litCounter;

	public float waitBetweenLight;

	private float waitBetweenCounter;

	private bool shouldBeLit;

	private bool shouldBeDim;

	public List <int> activeSequence;

	private int posInSequence;

	private bool gameActive;

	private int inputInSequence;

	public AudioSource correct;

	public AudioSource incorrect;

	public Text scoreText;

	// Use this for initialization
	void Start () {

		if (!PlayerPrefs.HasKey ("High Score")) {
			PlayerPrefs.SetInt ("High Score", 0);
		}

		scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt("High Score");

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

		if (shouldBeLit) {
			litCounter -= Time.deltaTime;
			if (litCounter < 0) {
				colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 0.5f);
				buttonSounds [activeSequence [posInSequence]].Stop();
				shouldBeLit = false;

				shouldBeDim = true;
				waitBetweenCounter = waitBetweenLight;

				posInSequence++;
			}
		}
		if (shouldBeDim) {
			waitBetweenCounter -= Time.deltaTime;

			if (posInSequence >= activeSequence.Count) {

				shouldBeDim = false;
				gameActive = true;

			} else {
				if (waitBetweenCounter > 0) {
					
					/*colorSelect = Random.Range (0, colors.Length);

					activeSequence.Add (colorSelect);*/

					colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);

					buttonSounds [activeSequence [posInSequence]].Play();

					litCounter = lit;
					shouldBeLit = true;
					shouldBeDim = false;
				}
			}
		}
	}

	public void StartGame () {

		activeSequence.Clear ();

		posInSequence = 0;

		colorSelect = Random.Range (0, colors.Length);

		activeSequence.Add (colorSelect);

		colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);

		buttonSounds [activeSequence [posInSequence]].Play();

		litCounter = lit;
		shouldBeLit = true;

		scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt("High Score");
	}

	public void ColourPressed(int whichButton){

		if (gameActive) {



			if (activeSequence[inputInSequence] == whichButton) {
				Debug.Log ("Correct");

				inputInSequence++;

				if (inputInSequence >= activeSequence.Count) {

					PlayerPrefs.SetInt ("High Score", activeSequence.Count);
				}

					if (activeSequence.Count > PlayerPrefs.GetInt("High Score")){

					scoreText.text = "Score: " + activeSequence.Count + "High Score" + PlayerPrefs.GetInt("High Score");

					posInSequence = 0;

					inputInSequence = 0;

					colorSelect = Random.Range (0, colors.Length);

					activeSequence.Add (colorSelect);

					colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);

					buttonSounds [activeSequence [posInSequence]].Play(); 

					litCounter = lit;
					shouldBeLit = true;

					gameActive = false;

					correct.Play ();

				}

			} else {
				Debug.Log ("Wrong");
				incorrect.Play ();
				gameActive = false;
			}
		}

	}
}

Edit: So I re-typed the code , and I’m not sure what was wrong, but it fixed itself.

I’m going to assume it’s because of your things like:

colorSelect = Random.Range (0, colors.Length);

Since you’re using it in: whatever[colorSelect] style, you should be using:

colorSelect = Random.Range (0, colors.Count - 1);

It’s out of range, because you’re most likely pulling Max Range + 1.

If you have 30 variables in an array, and you use Random.Range(0, whatever.Length/Count/Whatever), it can return: 30… However, when pulling from an array, the max is 29, since arrays start at 0.