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

Question Unity NPC Dialogue Script doesn't work

Discussion in 'Scripting' started by DasInnuendo, Dec 28, 2022.

  1. DasInnuendo

    DasInnuendo

    Joined:
    Mar 4, 2022
    Posts:
    2
    Hi, I followed a Tutorial on how to create an NPC Dialogue. But it isn't working. Everytime I go near the NPC, nothing shows up. Here's the script:

    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using Unity.VisualScripting;
    using UnityEngine;
    using UnityEngine.UI;

    public class NPCInteraction : MonoBehaviour
    {
    public GameObject DialoguePanel;
    public TextMeshProUGUI DialogueText;
    public string[] dialogue;
    private int index;
    public float wordSpeed;
    public bool PlayerIsClose;
    public GameObject contButton;

    void Start()
    {


    }

    void Update()
    {
    if(Input.GetKeyUp(KeyCode.E) && PlayerIsClose)
    {
    if(DialoguePanel.activeInHierarchy)
    {
    zeroText();
    }
    else
    {
    DialoguePanel.SetActive(true);
    StartCoroutine(Typing());
    }
    }
    if(DialogueText.text == dialogue[index])
    {
    contButton.SetActive(true);
    }
    }

    public void zeroText()
    {
    DialogueText.text = "";
    index = 0;
    DialoguePanel.SetActive(false);
    }

    IEnumerator Typing()
    {
    foreach(char letter in dialogue[index].ToCharArray())
    {
    DialogueText.text += letter;
    yield return new WaitForSeconds(wordSpeed);
    }
    }

    public void NewLine()
    {
    contButton.SetActive(false);
    if(index < dialogue.Length - 1)
    {
    index++;
    DialogueText.text = "";
    StartCoroutine(Typing());
    }
    else
    {
    zeroText();
    }
    }


    private void OnTriggerEnter(Collider other)
    {
    if(other.CompareTag("Player"))
    {
    PlayerIsClose = true;
    Debug.Log("Hi");
    }
    }

    private void OnTriggerExit(Collider other)
    {
    if (other.CompareTag("Player"))
    {
    PlayerIsClose = false;
    zeroText();
    }
    }
    }
     
  2. DasInnuendo

    DasInnuendo

    Joined:
    Mar 4, 2022
    Posts:
    2
    I watched this Tutorial: