Search Unity

Question I added a dialogue system but every time I restart the scene the text becomes invisible.

Discussion in 'Scripting' started by gianluca_unity, Sep 3, 2021.

  1. gianluca_unity

    gianluca_unity

    Joined:
    Jun 9, 2020
    Posts:
    2
    I added a dialogue system but every time I restart the scene the text becomes invisible. Below I wrote the code from which I check the text, I don't know if it's the fault of the code or else if it doesn't work. I followed this tutorial for the dialogue system. Click for the tutorial Here

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class dialog : MonoBehaviour
    7. {
    8.     public GameObject cartellino;
    9.  
    10.     public TextMeshProUGUI textDisplay;
    11.     public string[] sentences;
    12.     private int index;
    13.     public float TypingSpeed;
    14.  
    15.     public GameObject continueButton;
    16.  
    17.     IEnumerator Type(){
    18.  
    19.         foreach(char letter in sentences[index].ToCharArray()){
    20.             textDisplay.text += letter;
    21.             yield return new WaitForSeconds(TypingSpeed);
    22.  
    23.         }
    24.     }
    25.     void Update(){
    26.         if(textDisplay.text == sentences[index]){
    27.             continueButton.SetActive (true);
    28.             cartellino.SetActive (true);
    29.  
    30.         }
    31.     }
    32.  
    33.     // Start is called before the first frame update
    34.     void Start()
    35.     {
    36.         StartCoroutine(Type());
    37.     }
    38.  
    39.     public void  NextSentence(){
    40.         continueButton.SetActive (false);
    41.         if(index < sentences.Length -1){
    42.             index++;
    43.             textDisplay.text = "";
    44.             StartCoroutine(Type());
    45.         }else{
    46.             textDisplay.text = "";
    47.             cartellino.SetActive (false);
    48.         }
    49.     }
    50. }
    51.  
     
    Last edited: Sep 3, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You're welcome to have a look around mine, attached to this message as a .unitypackage, which supports HTML and color properly.

    If you want to debug the above, what is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     

    Attached Files: