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
{
}
}
}