Hey all! I have been following a YouTube tutorial on how to create text boxes for NPC’s since it has been a while and have run into a problem. When I talk to the first NPC it all works correctly however, when I approach the second NPC no text box occurs. Here are the scripts that link together along with a link to the tutorial I followed.
Also I have attached the scripts correctly since I just copy and pasted the first NPC to test this, the dialog is different so it should show up except, well it doesn’t. Anyway thanks for taking the time to look in advance!
Script that contains the dialog for the NPC (attached to each NPC.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dialogHolder : MonoBehaviour {
public string dialogue;
private DialogueManager dMan;
public string[] dialogueLines;
// Use this for initialization
void Start ()
{
dMan = FindObjectOfType<DialogueManager>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.name == "Player")
{
//dMan.ShowText(dialogue);
if (!dMan.dialogActive)
{
dMan.dialogLines = dialogueLines;
dMan.currentLine = 0;
dMan.ShowDialogue();
}
}
}
}
Dialog manager script (attached to canvas)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour {
public GameObject dBox;
public Text dText;
public bool dialogActive;
public string[] dialogLines;
public int currentLine;
private Player thePlayer;
// Use this for initialization
void Start () {
dBox.SetActive(false);
thePlayer = FindObjectOfType<Player>();
}
// Update is called once per frame
void Update ()
{
if (dialogActive && Input.GetKeyDown(KeyCode.Return))
{
//dBox.SetActive(false);
currentLine++;
}
if (currentLine >= dialogLines.Length)
{
dBox.SetActive(false);
currentLine = 0;
thePlayer.canMove = true;
}
dText.text = dialogLines[currentLine];
}
public void ShowText(string dialogue)
{
dialogActive = true;
dBox.SetActive(true);
dText.text = dialogue;
}
public void ShowDialogue()
{
dialogActive = true;
dBox.SetActive(true);
thePlayer.canMove = false;
}
}
Tutorial Link