Search Unity

Resolved Need help. TextMeshProUGUI text is empty but the text length is always 1. any solution ?

Discussion in 'UGUI & TextMesh Pro' started by SaniBerkovic, Aug 23, 2022.

  1. SaniBerkovic

    SaniBerkovic

    Joined:
    Jun 8, 2022
    Posts:
    19
    I'm using TextMeshPro 3.0.6

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using TMPro;
    6.  
    7. public class SavingGame : MonoBehaviour
    8. {
    9.     public int resWidth = 1920;
    10.     public int resHeight = 1080;
    11.     public SaveLoad saveLoad;
    12.     public Description description;
    13.     public TextMeshProUGUI savedGameDescriptionText;
    14.  
    15.     private static int countName;
    16.  
    17.     private void Start()
    18.     {
    19.         countName = 0;
    20.      
    21.         string[] dirs = Directory.GetDirectories(Application.persistentDataPath + "\\" + "Saved Screenshots",
    22.             "*.*", SearchOption.TopDirectoryOnly);
    23.  
    24.         if(dirs.Length > 0)
    25.         {
    26.             countName = dirs.Length;
    27.         }
    28.     }
    29.  
    30.     public static string ScreenShotName(int width, int height)
    31.     {
    32.         return string.Format("{0}/Saved Screenshots/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
    33.             Application.persistentDataPath,
    34.             countName,
    35.             width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if (Input.GetKeyDown("k"))
    41.         {
    42.             description.StartFading(true);
    43.         }
    44.     }
    45.  
    46.     public void Save()
    47.     {
    48.         var time = description.StartFading(false);
    49.  
    50.         StartCoroutine(StartSaving(time));
    51.     }
    52.  
    53.     IEnumerator StartSaving(float time)
    54.     {
    55.         yield return new WaitForSeconds(time);
    56.  
    57.         string filename = ScreenShotName(resWidth, resHeight);
    58.         string directory = Path.GetDirectoryName(filename);
    59.         Directory.CreateDirectory(directory);
    60.         ScreenCapture.CaptureScreenshot(filename);
    61.         string descriptionContent = savedGameDescriptionText.text;
    62.         if (savedGameDescriptionText.text.Length != 0)
    63.         {
    64.             string descriptionFileFolder = directory + "\\" + "Description.txt";
    65.             File.WriteAllText(descriptionFileFolder, descriptionContent);
    66.         }
    67.         StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
    68.  
    69.         countName++;
    70.     }
    71. }
    72.  
    No matter how i'm checking if the variable savedGameDescriptionText is empty it's never empty.

    I tried to check first only if savedGameDescriptionText is not != "" but even if it's "" it's entering.
    Then i tired with the text.Length != 0 but using a break point i see that the Length value is 1.

    In the editor i see that the text i s empty :

    Text Input is empty and i see with a breakpoint that savedGameDescirptionText is "" but the Length value is still 1 so the checking if is not empty always true :



    I can check if the Length is 1 like Length == 1 but that's not a solution.

    I'm pressing the K key to bring up the canvas changing the cnavas alpha from 0 to 1 then enable true the input field so i can type inside some text then when pressing the Save button it's changing back the canvas alpha from 1 to 0 and then it's calling the Save method. then i see that the Length value is 1.

    The Placeholder have text inside Enter description...
    but i'm not using the Placeholder in the script but the Saved Game Description Text.
     
  2. SaniBerkovic

    SaniBerkovic

    Joined:
    Jun 8, 2022
    Posts:
    19
    It's not a bug. i had to get direct access to the MP_InputField text and not to the text child.

    This is working :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using TMPro;
    6.  
    7. public class SavingGame : MonoBehaviour
    8. {
    9.     public int resWidth = 1920;
    10.     public int resHeight = 1080;
    11.     public SaveLoad saveLoad;
    12.     public Description description;
    13.     public TMP_InputField _inputField;
    14.  
    15.     private static int countName;
    16.  
    17.     private void Start()
    18.     {
    19.         countName = 0;
    20.        
    21.         string[] dirs = Directory.GetDirectories(Application.persistentDataPath + "\\" + "Saved Screenshots",
    22.             "*.*", SearchOption.TopDirectoryOnly);
    23.  
    24.         if(dirs.Length > 0)
    25.         {
    26.             countName = dirs.Length;
    27.         }
    28.     }
    29.  
    30.     public static string ScreenShotName(int width, int height)
    31.     {
    32.         return string.Format("{0}/Saved Screenshots/SaveSlot{1} SavedGameSlot_{2}x{3}_{4}/SavedGameSlot_{1}x{2}_{3}.png",
    33.             Application.persistentDataPath,
    34.             countName,
    35.             width, height, System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if (Input.GetKeyDown("k"))
    41.         {
    42.             description.StartFading(true);
    43.         }
    44.     }
    45.  
    46.     public void Save()
    47.     {
    48.         var time = description.StartFading(false);
    49.  
    50.         StartCoroutine(StartSaving(time));
    51.     }
    52.  
    53.     IEnumerator StartSaving(float time)
    54.     {
    55.         yield return new WaitForSeconds(time);
    56.  
    57.         string filename = ScreenShotName(resWidth, resHeight);
    58.         string directory = Path.GetDirectoryName(filename);
    59.         Directory.CreateDirectory(directory);
    60.         ScreenCapture.CaptureScreenshot(filename);
    61.         string descriptionContent = _inputField.text;
    62.         if (_inputField.text.Length != 0)
    63.         {
    64.             string descriptionFileFolder = directory + "\\" + "Description.txt";
    65.             File.WriteAllText(descriptionFileFolder, descriptionContent);
    66.         }
    67.         StartCoroutine(saveLoad.SaveWithTime(directory, Path.GetFileNameWithoutExtension(filename) + ".savegame.txt"));
    68.  
    69.         countName++;
    70.     }
    71. }
    72.  
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    As you have uncovered, you have to access the text property of the Input Field and not the child text components as those are used for display and do not represent the text itself.

    For instance when the text input field is set to password, the text property of the input field would contain the password while the child text component would contain something like "****".

    For formatting reasons, the child text component happens a zero width space <ZWSP> at the end of the text which is why its string has a length of 1 when the text has been cleared from the parent text input field.
     
    SaniBerkovic likes this.