Search Unity

Question Can't seem to figure out IndexOutOfRangeException on this basic script

Discussion in 'Scripting' started by jleven22, Mar 28, 2023.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I have been debugging the crap out of this dialogue system, but still getting a classic error:

    IndexOutOfRangeException: Index was outside the bounds of the array.


    It's really basic, so I feel like I might just be driving myself crazy and overlooking something. Here is the script in question:

    Code (CSharp):
    1.         if (canTalk && (PlayerController.instance.player.GetButtonUp("Fire") || PlayerController.instance.player.GetButtonUp("Dodge")) && DialogueManager.instance.isChatting)
    2.         {
    3.             dCounter++;
    4.  
    5.             if (DialogueManager.instance.isChatting)
    6.             {
    7.  
    8.                 if (dialogueSelection == 0)
    9.                 {
    10.                     DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];
    11.                 }
    12.                 if (dialogueSelection == 1)
    13.                 {
    14.                     DialogueManager.instance.dialogueText.text = dialogueLineB[dCounter];
    15.                 }
    16.                 if (dialogueSelection == 2)
    17.                 {
    18.                     DialogueManager.instance.dialogueText.text = dialogueLineC[dCounter];
    19.                 }
    20.             }
    21.  
    22.             AudioManager.instance.PlaySFX(dialogueSound);
    23.  
    24.         }

    In this case dialogueSelection == 0, so the error is happening at:

    DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];


    So wth am I doing wrong here? Oh and I should note the dialogue is being displayed as long as the player is triggering a collider:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.tag == "Player")
    4.         {
    5.             AudioManager.instance.PlaySFX(dialogueStartSound);
    6.             DialogueManager.instance.isChatting = true;
    7.  
    8.             if (dialogueSelection == 0)
    9.             {
    10.                 DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];
    11.             }
    12.             if (dialogueSelection == 1)
    13.             {
    14.                 DialogueManager.instance.dialogueText.text = dialogueLineB[dCounter];
    15.             }
    16.             if (dialogueSelection == 2)
    17.             {
    18.                 DialogueManager.instance.dialogueText.text = dialogueLineC[dCounter];
    19.             }
    20.  
    21.             DialogueManager.instance.dialogueContainer.SetActive(true);
    22.            
    23.  
    24.             PlayerController.instance.canWalk = false;
    25.             PlayerController.instance.cannotDodge = true;
    26.             PlayerController.instance.rangedArm.SetActive(false);
    27.            
    28.             PlayerController.instance.meleeArm.SetActive(false);
    29.  
    30.  
    31.             canTalk = true;
    32.  
    33.  
    34.         }
    35.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Some collection isn't as big as you think it is!

    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.
     
    jleven22 likes this.
  3. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    what is the value of the counter and the size of the list? you increment dCounter and then index a list. Safety checks and handling go a long way. you may want to do:
    Code (CSharp):
    1. var thing = myList[dCounter % myList.Count];
    if it meets your needs. if not, print all the info you need one line before getting the error, and see what doesnt meet your expectations (probably dCounter is getting larger than you expect or the list is missing items or you wanted a seperate counter for each list). Also worth verifying you dont have an unexpexted additional object that may be calling the function and incrementing the counter under your nose.
     
    jleven22 likes this.
  4. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I figured this out! And I really appreciate the support.

    Here's what I ended up with instead:

    Code (CSharp):
    1.             if (DialogueManager.instance.isChatting)
    2.             {
    3.  
    4.                 if (dialogueSelection == 0)
    5.                 {
    6.  
    7.                     if (dCounter < dialogueLine.Length)
    8.                     {
    9.                         DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];
    10.                         AudioManager.instance.PlaySFX(dialogueSound);
    11.                     }
    12.                 }
    13.                 if (dialogueSelection == 1)
    14.                 {
    15.                     if (dCounter < dialogueLineB.Length)
    16.                     {
    17.                         DialogueManager.instance.dialogueText.text = dialogueLineB[dCounter];
    18.                         AudioManager.instance.PlaySFX(dialogueSound);
    19.                     }
    20.                 }
    21.                 if (dialogueSelection == 2)
    22.                 {
    23.                     if (dCounter < dialogueLineC.Length)
    24.                     {
    25.                         DialogueManager.instance.dialogueText.text = dialogueLineC[dCounter];
    26.                     }
    27.                 }
    28.             }
    Error was mad about dCounter being larger than dialogueLine.Length, so I just added a qualifier in there and bing bang boom no more errors!