So I have 3 scripts after following a Brackeys tutorial and I want to be able to walk up to an NPC(or object) with the trigger attached and upon pressing ‘E’ the dialog opens
Dialogue code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue {
public string name;
[TextArea(3, 10)] // Minimum and maximum amount of lines the text will use
public string[] sentences;
}
Dialogue Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager2 : MonoBehaviour {
public Text nameText;
public Text dialogueText;
public GameObject dialogueBox;
private Queue<string> sentences;
// Use this for initialization
void Start () {
sentences = new Queue<string>();
}
public void StartDialogue (Dialogue dialogue)
{
Debug.Log("Starting conversation with" + dialogue.name);
nameText.text = dialogue.name;
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
Debug.Log(sentence);
}
void EndDialogue()
{
Debug.Log("End of conversation.");
}
// Update is called once per frame
void Update () {
}
}
Dialogue Trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour {
public Dialogue dialogue; // Dialogue script
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
FindObjectOfType<DialogueManager2>().StartDialogue(dialogue);
}
}
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager2>().StartDialogue(dialogue);
}
}