mafiaf
mafiaf

Reputation: 99

Dialogue keeps being stuck at last sentence

I made a dialogue script so that I can display sentences on different NPC's. I also have added a continue button so it goes from sentence to sentence.

The problem is that for some reason the last dialog gets stuck and keeps replaying if I interact after all sentences have played.

What I want is to replay all sentences that I added onto the NPC.

I also wanted to ask if there was a way to NOT use the continue button? And let the text type itself out after a certain amount of time. Like a real conversation.

As you can see I added the lines on the right Lines

The first line printed Lines

The second line printed

Lines

The third line printed Lines

Starts placing the last line back to back every time I interact UNLESS I click the Button, because then it clears. But it still replays the last sentence every time instead of reverting back to the 1st. Lines

Dialogue Script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

  [System.Serializable]
public class Dialogue2 : MonoBehaviour
{
    public GameObject dialogBox;  // Attach ui image to this
    public Text npcname;          // Attach UI text gameObject
    public Text dialogText;       // Attach UI text gameObject
    public string NPC;            // What is he/she called?
    public string[] sentences;
    public bool playerInRange;    // Is the Player in range?
    public float typingSpeed;
    private int index;

    public GameObject continueButton;



    public AudioSource SignUpSfx;
    public AudioSource SignDownSfx;


    void Update()
    {
      if(dialogText.text == sentences[index]){
        continueButton.SetActive(true);
      }

        // Player in range and E is hit
      if(Input.GetKeyDown(KeyCode.E) && playerInRange)
      {
        if(dialogBox.activeInHierarchy)
        {
          dialogBox.SetActive(false);
          SignDownSfx.Play();
        }

        else
        {
          dialogBox.SetActive(true);
          StopAllCoroutines();
          StartCoroutine(Type());
          npcname.text = NPC;
          SignUpSfx.Play();
        }
    }
}

    private void OnTriggerEnter2D(Collider2D other)
    {

      if(other.CompareTag("Entity"))
      {
        playerInRange = true;
      }
    }

    private void OnTriggerExit2D(Collider2D other)
    {

      if(other.CompareTag("Entity") && dialogBox.activeInHierarchy == true)
      {
        SignDownSfx.Play();
      }

      if(other.CompareTag("Entity"))
      {

        playerInRange = false;
        dialogBox.SetActive(false);
      }
    }

    IEnumerator Type(){

      foreach(char letter in sentences[index].ToCharArray()){
        dialogText.text += letter;
        yield return new WaitForSeconds(typingSpeed);

      }
    }

    public void NextSentence(){

      continueButton.SetActive(false);

      if(index < sentences.Length - 1){
        index++;
        dialogText.text = "";
        StartCoroutine(Type());
      } else {
        dialogText.text = "";
        continueButton.SetActive(false);

      }
    }

}


Upvotes: 0

Views: 327

Answers (1)

Penca53
Penca53

Reputation: 80

Couldn't you just change:

if(index < sentences.Length - 1){
        index++;
        dialogText.text = "";
        StartCoroutine(Type());
      } else {
        dialogText.text = "";
        continueButton.SetActive(false);

      }

To this?

if(index < sentences.Length - 1){
        index++;
        dialogText.text = "";
        StartCoroutine(Type());
      } else {
        dialogText.text = "";
        continueButton.SetActive(false);
        index = 0;
      }

Upvotes: 1

Related Questions