- Home /
Multiple NPCs
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 https://youtu.be/cVS9-nPmzGg
Answer by bobisgod234 · Apr 21, 2017 at 12:03 AM
The variable dialogActive is never set back to false, and your dialogHolder class wont call ShowDialogue() unless it is.
I have never seen this tutorial, but I assume you aresupposed to set it false when you disable the dialoge box:
if (currentLine >= dialogLines.Length)
{
dBox.SetActive(false);
dialogActive = false;
currentLine = 0;
thePlayer.canMove = true;
}
Thanks for taking the time to read through my code, you was correct. After inserting your code it is working perfectly, thank you!
Your answer
Follow this Question
Related Questions
How do i make a BattleLog? 0 Answers
How to change a notepad file for another notepad file? 1 Answer
How to display UI Text on GameObject in script? 0 Answers
My text is invisible if I put it under the Content object under a Scroll View 1 Answer
How do I call an DontDestroyOnLoad function on UI text 2 Answers