Search Unity

Question IndexOutOfRangeException Error

Discussion in 'Scripting' started by jfair24, Dec 13, 2022.

  1. jfair24

    jfair24

    Joined:
    Dec 13, 2022
    Posts:
    3
    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)


    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using TMPro;
    7.  
    8. public class Dialogue : MonoBehaviour
    9. {
    10.       public TextMeshProUGUI textComponent;
    11.     public string[] lines;
    12.     public float textSpeed;
    13.    
    14.     private int index;
    15.    // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        textComponent.text = string.Empty;
    19.        StartDialogue();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.       if(Input.GetMouseButtonDown(0))
    26.       {
    27.       NextLine();
    28.       }
    29.       else
    30.       {
    31.       StopAllCoroutines();
    32.       textComponent.text = lines[index];
    33.       }
    34.     }
    35.    
    36.     void StartDialogue()
    37.     {
    38.     index = 0;
    39.     StartCoroutine(TypeLine());
    40.     }
    41.    
    42.     IEnumerator TypeLine()
    43.     {
    44.         foreach (char c in lines[index].ToCharArray())
    45.         {
    46.             textComponent.text += c;
    47.             yield return new WaitForSeconds(textSpeed);
    48.         }
    49.     }
    50.    
    51.     void NextLine()
    52.     {
    53.         if (index < lines.Length - 1)
    54.         {
    55.             index++;
    56.             textComponent.text = string.Empty;
    57.             StartCoroutine(TypeLine());
    58.         }
    59.         else
    60.         {
    61.             gameObject.SetActive(false);
    62.         }
    63.     }
    64. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    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.)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    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 :)
     
  5. jfair24

    jfair24

    Joined:
    Dec 13, 2022
    Posts:
    3
    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!
     
  6. jfair24

    jfair24

    Joined:
    Dec 13, 2022
    Posts:
    3
    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:
    1. IEnumerator TypeLine()
    2. {
    3. foreach (char c in lines[index].ToCharArray())
    4. {
    5. textComponent.text += c;
    6. yield return new WaitForSeconds(textSpeed);
    7. }
    8. }
    Other than that, I don't know what it could be the cause of it.
     
  7. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    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.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    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.