- Home /
,C# question: how to add sentences via script
Brackeys taught me how to add dialogue via the inspector of a gameObject that has DialogueTrigger attached. The sentences are added to a dialogue objected which is sent to the Dialogue Manager.
However, I don't want to type my sentences in the inspector. I want to keep the array of sentences in the code. How do I add sentences to a dialogue object's array? How do I trigger it to "send" to the manager once a quest is complete? Thank you!!!
[System.Serializable]
public class Dialogue
{
[TextArea(3,10)]
public string[] sentences;
}
---------
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
void Start() {
TriggerDialogue(); {
}
}
public void TriggerDialogue() {
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
----------
public class DialogueManager : MonoBehaviour
{
public Text dialogueText;
public GameObject dialogueBox;
public Queue<string> sentences;
public GameObject moveButton;
void Start() {
sentences = new Queue<string>();
dialogueBox.SetActive(true);
moveButton.SetActive(false);
}
public void StartDialogue(Dialogue dialogue) {
sentences.Clear();
foreach(string sentence in dialogue.sentences) {
//fill the queue with the sentences[] array from dialogue
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence() {
if (sentences.Count == 0 ) {
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
public void EndDialogue() {
Debug.Log("End of dialogue");
dialogueBox.SetActive(false);
moveButton.SetActive(true);
}
}
,So Brackeys showed me how to send dialogue to the UI Text component. This code allows me to add sentences in the inspector of a gameObject with DialogueTrigger attached. The sentences are sent to DialogueManager.
However, I don't want to type the sentences into the inspector. I want the code to hold the sentences. I want to "trigger" the sentences when a quest is complete. So when the quest is complete, the sentences in the code will be sent to Dialogue Manager.
My question is, how do I fill a dialogue object's array with sentences? How do I "send" the dialogue object to the manager when the quest is complete? Thank you so much!!!
[System.Serializable]
public class Dialogue
{
[TextArea(3,10)]
public string[] sentences;
}
---------
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
void Start() {
TriggerDialogue(); {
}
}
public void TriggerDialogue() {
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
-------
public class DialogueManager : MonoBehaviour
{
public Text dialogueText;
public GameObject dialogueBox;
public Queue<string> sentences;
public GameObject moveButton;
void Start() {
sentences = new Queue<string>();
dialogueBox.SetActive(true);
moveButton.SetActive(false);
}
public void StartDialogue(Dialogue dialogue) {
sentences.Clear();
foreach(string sentence in dialogue.sentences) {
//fill the queue with the sentences[] array from dialogue
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence() {
if (sentences.Count == 0 ) {
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogueText.text = sentence;
}
public void EndDialogue() {
Debug.Log("End of dialogue");
dialogueBox.SetActive(false);
moveButton.SetActive(true);
}
}
Answer by jackmw94 · Dec 28, 2020 at 10:54 AM
Dialogue objects don’t have to be created in the inspector, you can create these where you want to show your hard coded dialogue.
Once you have a dialogue object, you already have the code to send this to the DialogueManager in the function TriggerDialogue in the class DialogueTrigger.
public void TriggerPostLevelDialogue()
{
// create new dialogue object
Dialogue dialogue = new Dialogue();
// set the data
dialogue.Sentences = new string[]{
“Well done, you completed the level”,
“I never doubted you!”
};
// trigger dialogue
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
This but I would stay clear of putting large amounts of text in your applications code. There are much better formants/ methods for storing and retrieving content than littering your code with constants and string literals. (i.e. serialised to files or a database)
Your answer
![](https://koobas.hobune.stream/wayback/20220613022159im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i create a conversion like whatsapp in unity3d 0 Answers
Executing Code by Scenes 1 Answer