IndexOutOfRangeException Error

Hey everyone,
I’m working on some pop-up Dialogue for a 2-D board game that I’m making for one of my classes. However, when I go to play the game, the console spits out a bunch of IndexOutOfRangeException errors. Keep in mind that I tried this exact project once before and it did not give me this error, and I did everything the same as the first version. And, ontop of the error, my text in the Canvas does not show up. I added by code down below, so if anyone sees an error, please let me know! (Also, I’m using Microsoft Dreamweaver, not VisualStudio if that makes a difference)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Dialogue : MonoBehaviour
{
      public TextMeshProUGUI textComponent;
    public string[] lines;
    public float textSpeed;
   
    private int index;
   // Start is called before the first frame update
    void Start()
    {
       textComponent.text = string.Empty;
       StartDialogue();
    }

    // Update is called once per frame
    void Update()
    {
      if(Input.GetMouseButtonDown(0))
      {
      NextLine();
      }
      else
      {
      StopAllCoroutines();
      textComponent.text = lines[index];
      }
    }
   
    void StartDialogue()
    {
    index = 0;
    StartCoroutine(TypeLine());
    }
   
    IEnumerator TypeLine()
    {
        foreach (char c in lines[index].ToCharArray())
        {
            textComponent.text += c;
            yield return new WaitForSeconds(textSpeed);
        }
    }
   
    void NextLine()
    {
        if (index < lines.Length - 1)
        {
            index++;
            textComponent.text = string.Empty;
            StartCoroutine(TypeLine());
        }
        else
        {
            gameObject.SetActive(false);
        }
    }
}

You could look at the actual content of lines, probably an empty array. So then it’s not the code, but the scene that has the issue. (Though the code could protect against the case there are no lines.)

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is (critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection
  • remember you might have more than one instance of this script in your scene/prefab

Knowing the line number would help quite a bit! Double-clicking the exception should take you to the offending line; failing that, you can look at the stack trace to see the path that was taken.

Make sure that you do your bounds-checking. Perhaps even index 0 is invalid (:

Thanks for the suggestion, but as of right now the only thing that is physically in the scene is a Panel I inserted to act as the Dialogue Box. Unless it’s something to do with the Canvas/Panel, I don’t think it’s something in the scene. I appreciate the suggestion though!

Thanks to everyone that’s trying to help. The problem is still not solved, but here’s some information I’ve gathered:
The error reads:
IndexOutOfRangeException: Index was outside the bounds of the array.
Dialogue.Update () (at Assets/Scripts/Dialogue.cs:30)

However, the only thing on line 30 is a {
So, I believe the error could potentially be coming from lines 42 - 49:

  • IEnumerator TypeLine()
  • {
  • foreach (char c in lines[index].ToCharArray())
  • {
  • textComponent.text += c;
  • yield return new WaitForSeconds(textSpeed);
  • }
  • }

Other than that, I don’t know what it could be the cause of it.

Don’t just guess, figure it out. Line 30 in the code you posted is not necessarily line 30 in your code. Go to your code, not the one pasted here, and look at line 30.

If you’re getting the error on line 30, then you either have made changes you didn’t save, or you didn’t run it to get an updated error line. Oh, actually as @RadRedPanda pointed out, if you actually look at your posted code, you have 2 blank spaces at the top of it. So yep, line 32 is actually line 30.

Since line 32 that is the source of the error. And since this is run in Update non-stop as long as the gameobject is active. Which would also explain why you’re getting a bunch of index out of range errors.

I would suggest making sure your lines array actually has something in it in the inspector or however you are populating it.

Debug.Log the value of lines[index]. Heck, Debug.log the length of your array. Print out all the values of everything if you need to.

Also, I’m not sure if this is a single script you are reusing. Are you changing the values of lines through code? If so, index currently never gets reset, thus if you say talk to npc 1 who has 10 lines but then talk to npc 2 that has 5 lines, index would still be at 9, thus it would be higher than the next group of lines.

But, since it sounds like it’s happening right at the start, your lines array is most likely empty.