Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

where do I trigger audio in this script and how do I end it when the dialouge is over.

Discussion in 'Scripting' started by switchluts, May 22, 2020.

  1. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    Code (CSharp):
    1. {
    2.     public Image image;
    3.     public TextMeshProUGUI dialogueText;
    4.     PlayerMove playerMove;
    5.  
    6.  
    7.     private Queue<string> sentences;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         playerMove = FindObjectOfType<PlayerMove>();
    13.         sentences = new Queue<string>();
    14.     }
    15.  
    16.     public void StartDialogue(Dialogue dialogue)
    17.     {
    18.  
    19.         playerMove.canMove = false;
    20.  
    21.         image.gameObject.SetActive(true);
    22.  
    23.         sentences.Clear();
    24.  
    25.         foreach (string sentence in dialogue.sentences)
    26.         {
    27.             sentences.Enqueue(sentence);
    28.         }
    29.  
    30.         DisplayNextSentence();
    31.     }
    32.  
    33.     public void DisplayNextSentence()
    34.     {
    35.         if (sentences.Count == 0)
    36.         {
    37.             EndDialogue();
    38.             return;
    39.         }
    40.  
    41.         string sentence = sentences.Dequeue();
    42.         StopAllCoroutines();
    43.         StartCoroutine(TypeSentence(sentence));
    44.     }
    45.  
    46.     IEnumerator TypeSentence(string sentence)
    47.     {
    48.         dialogueText.text = "";
    49.         foreach (char letter in sentence.ToCharArray())
    50.         {
    51.             dialogueText.text += letter;
    52.             yield return null;
    53.         }
    54.     }
    55.  
    56.     void EndDialogue()
    57.     {
    58.  
    59.     playerMove.canMove = true;
    60.         image.gameObject.SetActive(false);
    61.     }
    62.  
    63. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It's your game, only you know the correct time to play the audio.
     
  3. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    I want to play the audio while they are talking and stop it when they are not