Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Prevent Dialogue Spam for Unity UI

Discussion in 'UGUI & TextMesh Pro' started by Wolfgang385, May 3, 2024.

  1. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    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).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Guide_Dialogue : MonoBehaviour
    8. {
    9.  
    10.     public GameObject dialoguePanel;
    11.     public TextMeshProUGUI dialogueText;
    12.     public string[] dialogue;
    13.     private int index;
    14.  
    15.     public GameObject contButton;
    16.     public float wordSpeed;
    17.     public bool playerIsClose;
    18.  
    19.     public bool isTalking = false;
    20.     public bool buttonActive = false;
    21.  
    22.     private bool cooldown = false;
    23.  
    24.     // happens when the game starts
    25.     void Start()
    26.     {
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         // seeing if player is using inspect button while in range of guide
    34.         if(Input.GetKeyDown(KeyCode.E) && playerIsClose)
    35.         {
    36.             // clears dialogue box and resets NPC
    37.             if (dialoguePanel.activeInHierarchy)
    38.             {
    39.                 //zeroText();
    40.             }
    41.             else
    42.             {
    43.                 dialoguePanel.SetActive(true);
    44.                 isTalking = true;
    45.                 PlayerMovement.moveSpeed = 0f;
    46.                 StartCoroutine(Typing());
    47.             }
    48.         }
    49.  
    50.        
    51.         if(Input.GetKeyDown(KeyCode.E) && isTalking && buttonActive)
    52.         {
    53.             if (cooldown == false)
    54.             {
    55.                 StopCoroutine(Typing());
    56.                 NextLine();
    57.                 Invoke("ResetCooldown", 1.5f);
    58.                 cooldown = true;
    59.             }
    60.         }
    61.  
    62.         // once done, continue button becomes active
    63.         if (dialogueText.text == dialogue[index])
    64.         {
    65.             contButton.SetActive(true);
    66.             buttonActive = true;
    67.         }
    68.     }
    69.  
    70.     void ResetCooldown()
    71.     {
    72.         cooldown = false;
    73.     }
    74.  
    75.     // 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]
    76.     public void zeroText()
    77.     {
    78.         dialogueText.text = "";
    79.         PlayerMovement.moveSpeed = 5f;
    80.         isTalking = false;
    81.         index = 0;
    82.         buttonActive = false;
    83.         dialoguePanel.SetActive(false);
    84.     }
    85.  
    86.     // displays letters one at a time according to wordSpeed
    87.     IEnumerator Typing()
    88.     {
    89.         foreach(char letter in dialogue[index].ToCharArray())
    90.         {
    91.             dialogueText.text += letter;
    92.             yield return new WaitForSeconds(wordSpeed);
    93.         }
    94.     }
    95.    
    96.     // moves to the next line if there is a next line automatically [Can be changed to stop at certain points for cutscenes]
    97.     public void NextLine()
    98.     {
    99.  
    100.         contButton.SetActive(false);
    101.         buttonActive = false;
    102.  
    103.         if(index < dialogue.Length - 1)
    104.         {
    105.             index++;
    106.             dialogueText.text = "";
    107.             StartCoroutine(Typing());
    108.         }
    109.         else
    110.         {
    111.             zeroText();
    112.         }
    113.     }
    114.  
    115.     // if player is in box collider, they can speak to Guide
    116.     private void OnTriggerEnter2D(Collider2D other)
    117.     {
    118.         if (other.CompareTag("Player"))
    119.         {
    120.             playerIsClose = true;
    121.         }
    122.     }
    123.  
    124.     // if player leaves box collider, dialogue is stopped [or suspended based on how you change zeroText]
    125.     private void OnTriggerExit2D(Collider2D other)
    126.     {
    127.         if (other.CompareTag("Player"))
    128.         {
    129.             playerIsClose = false;
    130.             zeroText();
    131.         }
    132.     }
    133. }
    134.  
    upload_2024-5-2_17-4-31.png upload_2024-5-2_17-4-45.png
     
  2. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    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.

    upload_2024-5-2_18-7-40.png
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,405
    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:

    https://docs.unity3d.com/2023.1/Documentation/Manual/HOWTO-UIMultiResolution.html

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

    https://forum.unity.com/threads/inc...size-between-two-people.1130146/#post-7261747

    https://forum.unity.com/threads/game-ui-button-size-problem.1142650/#post-7337383

    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.

    https://gist.github.com/kurtdekker/8802b1b6c708637398f8c9167641efd3
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,695
    Please use the UGUI forum for UGUI questions. This is the 2D forum.

    I'll move this thread.