Dialogue system IndexOutOfRangeException: Index was outside the bounds of the array.

So I’m making a dialogue system for a visual novel type game and once I choose an option it displays this error: IndexOutOfRangeException: Index was outside the bounds of the array. I’ve been trying to solve it for some time now and really don’t know what to do.

Here’s my code:

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

public class Talking : MonoBehaviour
{
public GameObject Window;
public GameObject Character;
public Animator anim;

private int counter = 0;
private int FuckIndex = 100;

public GameObject contButton;
public Text dialogueText;
public string[ ] dialogue;
public int index = 0;
public int index1;
public int emotionchange;
public int firstOptions;

[Header(“Option 1”)]
public string[ ] option1dialogue;
public Text option1Text;
public GameObject opt1Button;
public GameObject opt1WindowButton;

[Header(“Option 2”)]
public string[ ] option2dialogue;
public Text option2Text;
public GameObject opt2Button;
public GameObject opt2WindowButton;

[Header(“Option 3”)]
public string[ ] option3dialogue;
public Text option3Text;
public GameObject opt3Button;
public GameObject opt3WindowButton;

public float wordSpeed;

void Start()
{
opt1WindowButton.SetActive(false);
opt2WindowButton.SetActive(false);
opt3WindowButton.SetActive(false);
anim = gameObject.GetComponent();
dialogueText.text = “”;
Window.SetActive(true);
Character.SetActive(true);
contButton.SetActive(false);
StartCoroutine(Typing());
}

public void Update()
{
if (counter == emotionchange)
{
anim.SetBool(“Surprise”, true);
}
else
{
anim.SetBool(“Surprise”, false);
}

if (counter == firstOptions)
{
opt1Button.SetActive(true);
opt2Button.SetActive(true);
opt3Button.SetActive(true);
}
else
{
opt1Button.SetActive(false);
opt2Button.SetActive(false);
opt3Button.SetActive(false);
}

}

public void Dissapear1()
{
opt2Button.SetActive(false);
opt3Button.SetActive(false);
counter += 1;
}

public void Dissapear2()
{
opt1Button.SetActive(false);
opt3Button.SetActive(false);
counter += 1;
}

public void Dissapear3()
{
opt1Button.SetActive(false);
opt2Button.SetActive(false);
counter += 1;
}

IEnumerator Typing()
{
foreach(char letter in dialogue[index].ToCharArray())
{
dialogueText.text += letter;
yield return new WaitForSeconds(wordSpeed);
}
contButton.SetActive(true);
counter += 1;
}

IEnumerator Typing1()
{
foreach(char letter in option1dialogue[index].ToCharArray())
{
option1Text.text += letter;
yield return new WaitForSeconds(wordSpeed);
}
index += 1;
opt1WindowButton.SetActive(true);
counter += 1;
}

IEnumerator Typing2()
{
foreach(char letter in option2dialogue[index].ToCharArray())
{
dialogueText.text += letter;
yield return new WaitForSeconds(wordSpeed);
}
opt2WindowButton.SetActive(true);
counter += 1;
}

IEnumerator Typing3()
{
foreach(char letter in option3dialogue[index].ToCharArray())
{
dialogueText.text += letter;
yield return new WaitForSeconds(wordSpeed);
}
opt3WindowButton.SetActive(true);
counter += 1;
}

public void NextLine()
{
contButton.SetActive(false);

if(index < dialogue.Length - 1)
{
index ++;
dialogueText.text = “”;
StartCoroutine(Typing());
}
else
{

}
}

public void NextLine1()
{
opt1Button.SetActive(false);

if(index < FuckIndex - 1)
{
index ++;
dialogueText.text = “”;
StartCoroutine(Typing1());
}
else
{

}
}

public void NextLine2()
{
opt2Button.SetActive(false);

if(index < option2dialogue.Length - 1)
{
index ++;
dialogueText.text = “”;
StartCoroutine(Typing2());
}
else
{

}
}

public void NextLine3()
{
opt3Button.SetActive(false);

if(index < option3dialogue.Length - 1)
{
index ++;
dialogueText.text = “”;
StartCoroutine(Typing3());
}
else
{

}
}

}

Please edit your post to use code-tags rather than plain text.

Devs cannot know which line you’re having the problem with so you should provide that information. An error saying that using an index on an array that is out of bounds is a pretty clear error that only you can debug TBH.

What have you done to try to solve it?

Here’s how to debug in Unity: Unity - Manual: Debug C# code in Unity

1 Like

Sorry, I’m new to this. The line is 116 so:
foreach(char letter in option1dialogue[index].ToCharArray())

My computer can’t download the file for debugging for some reason

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

Steps to success:

  • find which collection it is and what line of code accesses it <— critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection size
  • remember you might have more than one instance of this script in your scene/prefab
  • remember the collection may be used in more than one location in the code
  • remember that indices start at ZERO (0) and go to the count / length minus 1.

This means with three (3) elements, they are numbered 0, 1, and 2 only.

I dont follow… you HAVE the file that needs debugging, and you shouldnt really need any other tools than the IDE and unity.

That said, that code you posted is entirely to long to NOT use Code Tags.

See the little Code<> icon in the menu where you type. Press it, put your code in there. GL