I’m getting the classic error, but I’m not sure why.
IndexOutOfRangeException: Index was outside the bounds of the array.
Dialogue.Update () (at Assets/Scripts/Hub World/Dialogue.cs:57)
What could I be doing wrong here? Script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dialogue : MonoBehaviour
{
public string[] dialogueLine, dialogueLineB, dialogueLineC;
//public Collider2D dCollider;
private int dCounter;
private bool canTalk;
private int dialogueSelection, dialogueRangeHigh;
public int dialogueStartSound, dialogueStopSound, dialogueSound;
// Start is called before the first frame update
void Start()
{
if (dialogueLineC != null)
{
dialogueRangeHigh = 3;
}
if (dialogueLineB != null)
{
dialogueRangeHigh = 2;
}
if (dialogueLine != null)
{
dialogueRangeHigh = 1;
}
dialogueSelection = Random.Range(0, dialogueRangeHigh);
dCounter = 0;
canTalk = false;
}
// Update is called once per frame
void Update()
{
if (canTalk && (PlayerController.instance.player.GetButtonUp("Fire") || PlayerController.instance.player.GetButtonUp("Dodge")) && DialogueManager.instance.isChatting)
{
dCounter++;
if (DialogueManager.instance.isChatting)
{
if (dialogueSelection == 0)
{
DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];
}
if (dialogueSelection == 1)
{
DialogueManager.instance.dialogueText.text = dialogueLineB[dCounter];
}
if (dialogueSelection == 2)
{
DialogueManager.instance.dialogueText.text = dialogueLineC[dCounter];
}
}
AudioManager.instance.PlaySFX(dialogueSound);
}
if (PlayerController.instance.player.GetButtonUp("Cancel") && DialogueManager.instance.isChatting)
{
dCounter = dialogueLine.Length;
if (dialogueSelection == 0)
{
dCounter = dialogueLine.Length;
}
if (dialogueSelection == 1)
{
dCounter = dialogueLineB.Length;
}
if (dialogueSelection == 2)
{
dCounter = dialogueLineC.Length;
}
}
if (dCounter > dialogueLine.Length - 1 && DialogueManager.instance.isChatting)
{
AudioManager.instance.PlaySFX(dialogueStopSound);
DialogueManager.instance.isChatting = false;
DialogueManager.instance.dialogueContainer.SetActive(false);
dCounter = 0;
canTalk = false;
//Time.timeScale = 1f;
PlayerController.instance.canWalk = true;
if (!PlayerController.instance.isHub)
{
PlayerController.instance.cannotDodge = false;
PlayerController.instance.rangedArm.SetActive(true);
PlayerController.instance.meleeArm.SetActive(true);
}
}
//Debug.Log("dCounter = " + dCounter);
//Debug.Log(dialogueLine.Length);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
AudioManager.instance.PlaySFX(dialogueStartSound);
DialogueManager.instance.isChatting = true;
if (dialogueSelection == 0)
{
DialogueManager.instance.dialogueText.text = dialogueLine[dCounter];
}
if (dialogueSelection == 1)
{
DialogueManager.instance.dialogueText.text = dialogueLineB[dCounter];
}
if (dialogueSelection == 2)
{
DialogueManager.instance.dialogueText.text = dialogueLineC[dCounter];
}
//HubUIController.instance.DisplayDialogue();
DialogueManager.instance.dialogueContainer.SetActive(true);
//Time.timeScale = 0f;
PlayerController.instance.canWalk = false;
PlayerController.instance.cannotDodge = true;
PlayerController.instance.rangedArm.SetActive(false);
PlayerController.instance.meleeArm.SetActive(false);
//PlayerController.instance.meleeArm.SetActive(false);
canTalk = true;
/*GameObject[] allFX = GameObject.FindGameObjectsWithTag("FX");
for (int i = 0; i < allFX.Length; i++)
{
allFX[i].SetActive(false);
}*/
/*if (Input.anyKeyDown)
{
Debug.Log("next line");
dCounter++;
}*/
}
}
}