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