Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by JaimeCV, Mar 9, 2023.

  1. JaimeCV

    JaimeCV

    Joined:
    Feb 27, 2022
    Posts:
    2
    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 F***Index = 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<Animator>();
    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 < F***Index - 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
    {

    }
    }


    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html
     
    Kurt-Dekker likes this.
  3. JaimeCV

    JaimeCV

    Joined:
    Feb 27, 2022
    Posts:
    2
    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
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    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