Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Error / Issue setting Text using a custom list

Discussion in 'UGUI & TextMesh Pro' started by HyruleDefender, Aug 31, 2022.

  1. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    Hello Hello,

    I am attempting to set some TextMeshPro text using a custom list, but there seems to be an error, issue, or bug that is causing it to not work (unless I misunderstand something)

    Here's a screenshot (I'm currently customizing one of the unity learning games)
    upload_2022-8-30_21-24-9.png

    I have my custom list which takes 3 variables
    - string dialogue (what we want to say)
    - string speaker (who is saying it)
    - color color (the color we want to assign to those texts)

    THE ISSUE
    __________

    I can set the text directly like this in start or update
    tmp_Dialogue.text = "hello| "


    And I can even get it to work when I do it in start or update
    tmp_Dialogue.text = list_DialogueDict[int_DialogueID].dialogue;


    But calling the code through my function causes the dialogue to show BLANK even though the inspector shows what I want
    upload_2022-8-30_21-40-16.png

    Anyways, I'll dump my script, and any help is appreciated if I'm missing something

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. /// <summary>
    9. /// <para> UI Text Manager references all of the important UI for buttons, text, and portraits that may show during conversations </para>
    10. /// </summary>
    11.  
    12.     [System.Serializable]
    13. public class UITextManager : MonoBehaviour
    14. {
    15.  
    16.    //dialogue text obj & speaker text obj
    17.    [SerializeField] private TextMeshProUGUI tmp_Dialogue, tmp_Speaker;
    18.  
    19.     //dictionary list of conversational elements to add
    20.    [SerializeField] public List<DialogueDictionary> list_DialogueDict = new List<DialogueDictionary>();
    21.     // the dialogue ID we're on
    22.     private int int_DialogueID;
    23.  
    24.     private string _testString = "testing...";
    25.  
    26.     // options
    27.  
    28.     // a function immediately change and update the dialogue
    29.     public void UpdateDialogue(string _newDialogue, string _newSpeaker, Color _speakerColor) ///THIS FUNCTION DOES NOT PROPERLY SET THE INFORMATION
    30.     {
    31.         // make sure root is enabled
    32.         tmp_Dialogue.text = _newDialogue; /// doing this or hard setting it here breaks the text
    33.         tmp_Dialogue.color = _speakerColor;
    34.         tmp_Speaker.text = _newSpeaker;
    35.         tmp_Speaker.color = _speakerColor;
    36.         print("Just passed: " + list_DialogueDict[int_DialogueID].dialogue); /// BUT THIS DOES PROPERLY PRINT THE INFORMATION
    37.     }
    38.  
    39.     // a function to add dialogue to a list that needs to be read through
    40.     public void AddDialogueToQueue(string _newDialogue, string _newSpeaker, Color _speakerColor)
    41.     {
    42.  
    43.     }
    44.  
    45.     // Start is called before the first frame update
    46.     void Start()
    47.     {
    48.         ///THESE ALL WORK WHEN IN THE START FUNCTION
    49.         //tmp_Dialogue.text = "hello| ";
    50.         //tmp_Dialogue.text = list_DialogueDict[int_DialogueID].dialogue;
    51.         //tmp_Dialogue.text = "hello| " + list_DialogueDict[int_DialogueID].dialogue;
    52.  
    53.  
    54.         //tmp_Dialogue.color = list_DialogueDict[int_DialogueID].speakerColor;
    55.         //tmp_Speaker.text = "test| " + list_DialogueDict[int_DialogueID].speaker;
    56.         //tmp_Speaker.color = list_DialogueDict[int_DialogueID].speakerColor;
    57.  
    58.         /// THIS DOES NOT WORK ANYWHERE
    59.         UpdateDialogue(list_DialogueDict[int_DialogueID].dialogue, list_DialogueDict[int_DialogueID].speaker, list_DialogueDict[int_DialogueID].speakerColor); //ISSUE
    60.     }
    61.  
    62.     // Update is called once per frame
    63.     void Update()
    64.     {
    65.         if (Input.GetKeyDown("space")) { int_DialogueID++; UpdateDialogue(list_DialogueDict[int_DialogueID].dialogue, list_DialogueDict[int_DialogueID].speaker, list_DialogueDict[int_DialogueID].speakerColor); }
    66.     }
    67.  
    68. }//end of UITextManager
    69.  
    70.  
    71. // the custom list we'll use toe add dialogue
    72. [System.Serializable]
    73. public class DialogueDictionary
    74. {
    75.     public string dialogue, speaker;
    76.     public Color speakerColor;
    77.     // maybe add an image / sprite to show on each speaker?
    78.     //maybe add font for each situation / speaker?
    79.  
    80.     public DialogueDictionary(string _newDialogue, string _newSpeaker, Color _speakerColor)
    81.     {
    82.         dialogue = _newDialogue;
    83.         speaker = _newSpeaker;
    84.         speakerColor = _speakerColor;
    85.     }
    86.  
    87. }//end of dialogue dictionary
     
  2. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    Just following up, here are some processes that were confirmed and tested work.

    using the function with unique strings
    UpdateDialogue("sample speech", "sample speaker", Color.cyan);


    hard setting the individual variables (using the dictionary)

    Code (CSharp):
    1. //tmp_Dialogue.text = "hello| ";
    2. //tmp_Dialogue.text = list_DialogueDict[int_DialogueID].dialogue;
    3. //tmp_Dialogue.text = "hello| " + list_DialogueDict[int_DialogueID].dialogue;
     
  3. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    I also tried creating private root variables that can be used, but this doesnt seem to work either. It's a bummer because I am having trouble figuring out a workaround to this

    this still doesn't work
    upload_2022-9-16_21-43-6.png
     
  4. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    I thought I found a workaround to this.

    If I keep a local string, I can see the reference!


    string _sample = list_DialogueDict[int_DialogueID].dialogue;
    UpdateDialogue(_sample, "sample speaker", Color.cyan);


    upload_2022-9-17_10-18-16.png

    But this doesn't work in a scalable way. I tried setting all the local references in the the function to UpdateDialogue but it seems that there is some major issue with this transfer of information. I'm wondering if it has to do with my custom list (confusingly titled) DialogueDictionary


    Code (CSharp):
    1.  // a function immediately change and update the dialogue
    2.     public void UpdateDialogue(string _newDialogue, string _newSpeaker, Color _newColor) ///THIS FUNCTION DOES NOT PROPERLY SET THE INFORMATION
    3.     {
    4.         // make sure root is enabled
    5.  
    6.         //local variables (needed because directly setting causes a glitch with TMPro)
    7.         //string localDialogue = _newDialogue;
    8.         //string localSpeaker = _newSpeaker;
    9.         //Color localColor = _newColor;
    10.  
    11.         string localDialogue = list_DialogueDict[int_DialogueID].dialogue;
    12.         string localSpeaker = list_DialogueDict[int_DialogueID].speaker;
    13.         Color localColor = list_DialogueDict[int_DialogueID].speakerColor;
    14.  
    15.  
    16.         tmp_Dialogue.text = localDialogue; /// doing this or hard setting it here breaks the text
    17.         tmp_Dialogue.color = _newColor;
    18.         tmp_Speaker.text = localSpeaker;
    19.         tmp_Speaker.color = _newColor;
    20.         print("Just passed:" + "\n" + _newDialogue + "\n" + _newSpeaker + "\n" + _newColor); /// BUT THIS DOES PROPERLY PRINT THE INFORMATION
    21.     }
     
  5. HyruleDefender

    HyruleDefender

    Joined:
    Apr 4, 2017
    Posts:
    17
    Just following up that I reported this to Unity and it was confirmed as an issue