I’m following along with a Unity 2D RPG course on Udemy and I’m having a couple issues. The errors I’m getting are:
IndexOutOfRangeException: Index was outside the bounds of the array. DialogManager.Update () (at Assets/Scripts/DialogManager.cs:36) & Can’t rename to empty name UnityEditor.ProjectWindowUtil:CreateScriptAssetFromTemplateFile(String, String).
The script it appears to be hung up on is my Dialog Manager script which is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogManager : MonoBehaviour
{
public Text dialogText;
public Text nameText;
public GameObject dialogBox;
public GameObject nameBox;
public string[] dialogLines;
public int currentLine;
public static DialogManager instance;
// Start is called before the first frame update
void Start()
{
instance = this;
//dialogText.text = dialogLines[currentLine];
}
// Update is called once per frame
void Update()
{
if(dialogBox.activeInHierarchy)
{
if(Input.GetButtonUp("Fire1"))
{
currentLine++;
if(currentLine >= dialogLines.Length)
{
dialogBox.SetActive(false);
} else
{
dialogText.text = dialogLines[currentLine];
}
}
}
}
public void ShowDialog(string[] newLines)
{
dialogLines = newLines;
currentLine = 0;
dialogText.text = dialogLines[0];
dialogBox.SetActive(true);
}
}
Basically what happens is when I approach an NPC I should get a dialog box and response. My trigger is set in the inspector on the Collider but nothing happens and my canActivate trigger never activates when I walk inside the area. Visual Studio shows no errors. But it appears the hang up is on line 36 with currentLine++
I’m almost positive all my spelling and capitalization is correct so I’m not really sure what else to do. There’s also a Dialog Activator script but I don’t think that’s causing any issues. Just in case I’ve added that as well…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogActivator : MonoBehaviour
{
public string[] lines;
private bool canActivate;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(canActivate && Input.GetButtonDown("Fire1") && !DialogManager.instance.dialogBox.activeInHierarchy)
{
DialogManager.instance.ShowDialog(lines);
}
}
private void OnTriggerEnter2d(Collider2D other)
{
if(other.tag == "Player")
{
canActivate = true;
}
}
private void OnTriggerExit2d(Collider2D other)
{
if(other.tag == "Player")
{
canActivate = false;
}
}
}