Search Unity

Get TextMeshProUGUI text to display series of strings in separate array? Dialogue System.

Discussion in 'Scripting' started by TheDuples, Feb 14, 2019.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    30
    Right now I have a simple trigger enter and exit script in which if the player enters an area in front of an NPC, a text GUI object will appear on screen, representing the NPC's dialogue.

    Code (CSharp):
    1. public class DialogueInteraction : MonoBehaviour
    2. {
    3.     public Dialogue dialogue;
    4.  
    5.     public TextMeshProUGUI dialogueText;
    6.  
    7.     private bool triggerEntered = false;
    8.  
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetButtonDown("Interact") && triggerEntered == true)
    13.         {
    14.             dialogueText.enabled = true;
    15.             dialogueText.text =
    16.         }
    17.         else if (triggerEntered == false)
    18.         {
    19.             dialogueText.enabled = false;
    20.         }
    21.     }
    22.  
    23.     private void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.gameObject.tag == "Player")
    26.         {
    27.             triggerEntered = true;
    28.         }
    29.     }
    30.  
    31.     private void OnTriggerExit(Collider other)
    32.     {
    33.         if (other.gameObject.tag == "Player")
    34.         {
    35.             triggerEntered = false;
    36.         }
    37.     }
    38. }
    I have a reference to a separate script called Dialogue, which contains parameters for what would be the dialogue itself in an array of strings:

    Code (CSharp):
    1. [System.Serializable]
    2. public class Dialogue {
    3.  
    4.     [TextArea(3, 10)]
    5.     public string[] sentences;
    6.  
    7. }
    My question is how I can get my Text in the first script to display the series of strings as they are determined in the editor? The DialogueInteraction script would be attached to the NPC, and I want it so that if the player presses the interaction button, the text object will appear and display the series of strings in the Dialogue script in order, one at a time. I've searched lots of places online but there seems to be a serious lack of information on dialogue systems that aren't pre-made assets.
     
  2. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    30
    Please help.