IndexOutOfRangeException: Array index is out of range.
I'm practicing activating and deactivating dialog boxes and I'm following this video tutorial : https://www.youtube.com/watch?v=7KNQYPcx-uU
but somehow i cannot get the dialog to pop up and i feel like it is because of this error??
This is the error:
IndexOutOfRangeException: Array index is out of range. TextBoxManager.Update () (at Assets/Scripts/TextBoxManager.cs:43)
This is the code with error:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextBoxManager : MonoBehaviour {
public GameObject textBox;
public Text theText;
public TextAsset textfile;
public string[] textlines;
public int currentline;
public int endAtLine;
public playercontroller player;
public bool isActive;
public bool stopPlayerMovement;
// Use this for initialization
void Start () {
player = FindObjectOfType<playercontroller> ();
if(textfile != null)
{
textlines = (textfile.text.Split ('\n'));
}
if (endAtLine == 0) {
endAtLine = textlines.Length - 1;
}
if (isActive) {
EnableTextBox ();
}
else {
DisableTextBox ();
}
}
void Update(){
if (!isActive) {
return;
}
theText.text = textlines [currentline];
if (Input.GetKeyDown (KeyCode.Space)) {
currentline += 1;
}
if (currentline > endAtLine) {
DisableTextBox ();
}
}
public void EnableTextBox(){
textBox.SetActive (true);
isActive = true;
if (stopPlayerMovement) {
player.canmove = false;
}
}
public void DisableTextBox(){
textBox.SetActive (false);
isActive = false;
player.canmove = true;
}
public void ReloadScript(TextAsset theText)
{
if (theText != null) {
textlines = new string[1];
textlines = (theText.text.Split ('\n'));
}
}
}
and this is the other code that I'm using: using UnityEngine; using System.Collections;
public class ActivateTextAtLine : MonoBehaviour {
public TextAsset theText;
public int startline;
public int endline;
public TextBoxManager theTextBox;
public bool destroyWhenActivated;
public bool requireButtonPress;
private bool waitForPress;
// Use this for initialization
void Start () {
theTextBox = FindObjectOfType<TextBoxManager> ();
}
// Update is called once per frame
void Update () {
if (waitForPress && Input.GetKeyDown (KeyCode.K)) {
theTextBox.ReloadScript (theText);
theTextBox.currentline = startline;
theTextBox.endAtLine = endline;
theTextBox.EnableTextBox ();
if(destroyWhenActivated)
{
Destroy(gameObject);
}
}
}
void OnTriggerEnter2d(Collider2D other){
if (other.name == "Player") {
if (requireButtonPress) {
waitForPress = true;
return;
}
theTextBox.ReloadScript (theText);
theTextBox.currentline = startline;
theTextBox.endAtLine = endline;
theTextBox.EnableTextBox ();
if(destroyWhenActivated)
{
Destroy(gameObject);
}
}
}
void onTriggerExit2d(Collider2D other)
{
if (other.name == "Player") {
waitForPress = false;
}
}
}
thank you
Answer by TBruce · Oct 02, 2016 at 07:27 PM
TextBoxManager
looks fine. I added some checks to the file which will log errors instead of a crash to notify you where the problem occurs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextBoxManager : MonoBehaviour
{
public GameObject textBox;
public Text theText;
public TextAsset textfile;
public string[] textlines;
public int currentline;
public int endAtLine;
public playercontroller player;
public bool isActive;
public bool stopPlayerMovement;
// Use this for initialization
void Start ()
{
player = FindObjectOfType<playercontroller> ();
if(textfile != null)
{
textlines = (textfile.text.Split ('\n'));
}
if (endAtLine == 0)
{
endAtLine = textlines.Length - 1;
}
if (isActive)
{
if (textbox == null)
{
Debug.LogError("TextBoxManager.Start.EnableTextBox: textbox is null. Please replace and try again.");
}
EnableTextBox ();
}
else
{
if (textbox == null)
{
Debug.LogError("TextBoxManager.Start.DisableTextBox: textbox is null. Please replace and try again.");
}
DisableTextBox ();
}
}
void Update()
{
if (!isActive)
{
return;
}
theText.text = textlines [currentline];
if (Input.GetKeyDown (KeyCode.Space))
{
currentline += 1;
}
if (currentline > endAtLine)
{
if (textbox == null)
{
Debug.LogError("TextBoxManager.Update.DisableTextBox: textbox is null. Please replace and try again.");
return;
}
DisableTextBox ();
}
}
public void EnableTextBox()
{
textBox.SetActive (true);
isActive = true;
if (stopPlayerMovement)
{
player.canmove = false;
}
}
public void DisableTextBox()
{
textBox.SetActive (false);
isActive = false;
player.canmove = true;
}
public void ReloadScript(TextAsset theText)
{
if (theText != null)
{
textlines = new string[1];
textlines = (theText.text.Split ('\n'));
}
}
}
However in ActivateTextAtLine
this line could be a problem if you have destroyWhenActivated.
Destroy(gameObject);
Is ActivateTextAtLine
attached to the same game object the same game object as the TextBoxManager.textBox
object? It is difficult to tell.
Also, do you have destroyWhenActivated
set to true?
@wlrnd Could you please be so kind as to click the "Accept" button above to accept the answer if you have found this helpful?
Your answer
![](https://koobas.hobune.stream/wayback/20220612093857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What this error? 0 Answers
The shooting mechanics 0 Answers
Unty 3D C# Load other scene when bool = true, otherwise perform teleportation as commanded. 1 Answer
NavMesh always makes player move at origin on startup 1 Answer
Can someone please tell me whats wrong with this?, because when i put it says error in the GetAxis 1 Answer