IndexOutOfRangeException: Array index is out of range.

I’m practicing activating and deactivating dialog boxes and I’m following this video tutorial : Unity Basics - Activate & De-Activate Dialogue Box! - Creating a Dialogue Box Part 2 - YouTube

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 ('

'));

		}

		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 ('

'));

		}
	}


}

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

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 ('

'));
}

        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 ('

'));
}
}
}
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?