Prevent Dialogue Spam for Unity UI

Hi guys. So I’m having this issue where the dialogue in the dialogue panel is working but the dialogue panel (Unity UI Panel) itself isn’t working. It’s showing a transparent background and I’ve tried replacing the dialogue panel, changing the color, etc, but nothing has been working. Dialogue is a new subject for me so any advice is much appreciated (screenshots for reference below with the code to control the dialogue panel).

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

public class Guide_Dialogue : MonoBehaviour
{

    public GameObject dialoguePanel;
    public TextMeshProUGUI dialogueText;
    public string[] dialogue;
    private int index;

    public GameObject contButton;
    public float wordSpeed;
    public bool playerIsClose;

    public bool isTalking = false;
    public bool buttonActive = false;

    private bool cooldown = false;

    // happens when the game starts
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // seeing if player is using inspect button while in range of guide
        if(Input.GetKeyDown(KeyCode.E) && playerIsClose)
        {
            // clears dialogue box and resets NPC
            if (dialoguePanel.activeInHierarchy)
            {
                //zeroText();
            }
            else
            {
                dialoguePanel.SetActive(true);
                isTalking = true;
                PlayerMovement.moveSpeed = 0f;
                StartCoroutine(Typing());
            }
        }

       
        if(Input.GetKeyDown(KeyCode.E) && isTalking && buttonActive)
        {
            if (cooldown == false)
            {
                StopCoroutine(Typing());
                NextLine();
                Invoke("ResetCooldown", 1.5f);
                cooldown = true;
            }
        }

        // once done, continue button becomes active
        if (dialogueText.text == dialogue[index])
        {
            contButton.SetActive(true);
            buttonActive = true;
        }
    }

    void ResetCooldown()
    {
        cooldown = false;
    }

    // clears text box and resets index to 0 so it starts from the first line again [Can change so it doesn't set index to 0]
    public void zeroText()
    {
        dialogueText.text = "";
        PlayerMovement.moveSpeed = 5f;
        isTalking = false;
        index = 0;
        buttonActive = false;
        dialoguePanel.SetActive(false);
    }

    // displays letters one at a time according to wordSpeed
    IEnumerator Typing()
    {
        foreach(char letter in dialogue[index].ToCharArray())
        {
            dialogueText.text += letter;
            yield return new WaitForSeconds(wordSpeed);
        }
    }
   
    // moves to the next line if there is a next line automatically [Can be changed to stop at certain points for cutscenes]
    public void NextLine()
    {

        contButton.SetActive(false);
        buttonActive = false;

        if(index < dialogue.Length - 1)
        {
            index++;
            dialogueText.text = "";
            StartCoroutine(Typing());
        }
        else
        {
            zeroText();
        }
    }

    // if player is in box collider, they can speak to Guide
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            playerIsClose = true;
        }
    }

    // if player leaves box collider, dialogue is stopped [or suspended based on how you change zeroText]
    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            playerIsClose = false;
            zeroText();
        }
    }
}

9811278--1409097--upload_2024-5-2_17-4-45.png

I realize now that I had accidentally used the title to another problem I was having. Sorry for the confusion but the proper title should be something like: Make UI Panel Appear Unity UI. I also put another screenshot below of the actual problem for anyone interested.

First guess would be defective anchoring causing some or all of your RectTransforms to turn inside out or go far offscreen.

Prove it by running the game, pressing pause when the dialog is up and looking through the scene, just basic good old live scene debug inspection in Unity.

If it is anchoring / scaling, then…

Here are the official docs on how to make your UI handle various resolutions and aspect ratios cleanly:

Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:

Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.

I also use this CanvasScalerOrientationDriver utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.

Please use the UGUI forum for UGUI questions. This is the 2D forum.

I’ll move this thread.