I know this is probably super obvious to everyone else, but I’ve been stuck for days trying to create dialogue in Unity2D. I have a dialogue box that runs and works on startup. However no matter what I do I can’t get that same dialogue box to appear when I interact with an NPC. I have tried adding an on click event and button to it to no avail. I currently have the dialogue trigger and a button connected to the NPC. Any help is really appreciated.
public class DialogueManager : MonoBehaviour
{
public Text nameText;
public Text dialogueText;
public Animator animator;
private Queue<string> sentences; //Queues are first in, first out for data
// Start is called before the first frame update
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
animator.SetBool("isOpen", true);
nameText.text = dialogue.name;
sentences.Clear(); //gets rid of previous sentences so new ones can be loaded
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence ()
{
if (sentences.Count == 0) //checks to see if there are anymore sentences
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
StopAllCoroutines();
StartCoroutine(TypeSentence(sentence));
}
IEnumerator TypeSentence (string sentence) //this function uses an array to make one letter appear at a time
{ //may not be working, add larger delay between letters and check
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return null;
}
}
void EndDialogue()
{
Debug.Log("End of Convo");
animator.SetBool("isOpen", false);
}
}
//to make a caharacter speak, add the dialougue trigger script to that game object
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialouge()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "npc") //attempting to make npc speak on collision
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
Debug.Log("Convo w/ Frog");
}
}
}