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
  3. Dismiss Notice

Question Problem with TMPro text when running a game build

Discussion in 'Editor & General Support' started by MEGELUL, May 23, 2020.

  1. MEGELUL

    MEGELUL

    Joined:
    Feb 6, 2020
    Posts:
    3
    Hello, I tried posting this to Unity Answers, but got a captcha bug every time i tried, so I'll ask here.

    I'm new to Unity (and to programming in general) and am making my first game atm. It's a very simple game but I decided to incorporate some UI features for practice purposes.

    Everything works perfectly fine in the Unity editor, but when I build my game and launch it, some parts of it don't work as they should. After a few hours of digging around and examining the logs, I found the source of the problem - my game doesn't access the necessary TMPro text.

    Some of the scripts (i use C#) in my program use the text values of TMPro text labels. For example, I have a script that is responsible for letting the user rebind the keys while the game is running. It works the following way: when a user clicks the button with the corresponding command text, that button's text becomes "press any key". Then when a user presses any key, another method looks up, which of the buttons has the text value "press any key" and replaces the appropriate keybinding and button text with the key that was pressed. All of my buttons are TextMeshPro labels with an added button component. Here's how it looks like:




    As I said, everything works perfectly fine in the editor, but when the game launches, none of my rebinding commands worked. I checked why by logging what the values of all the different components were when the buttons were clicked and discovered that all of my button's TMPro.text value returned "Temp" and not what was on the screen. I checked the editor and found out that Temp was the text entered in each button's text field in the editor. I removed it, but then instead of Temp the value became just an empty string. When my game loads this script is supposed to populate the TMPro elements with text values of the player controls (set with another script, which runs first) and in the editor my scripts use those values, but in the build, they use the values set in the editor window.

    Here is the code of my script for rebinding keys:

    Code (CSharp):
    1.     using System.Collections;
    2.     using UnityEngine;
    3.     using TMPro;
    4.    
    5.     public class ChangeKey : MonoBehaviour
    6.     {
    7.         public TextMeshProUGUI[] keys = new TextMeshProUGUI[5];
    8.         private bool waitingForInput = false;
    9.         private string temp;
    10.         private Event e;
    11.         public GameObject gameSettings;
    12.         public GameObject audioManager;
    13.         private string keyToSave;
    14.         private string[] savedKeyNames = { "PlayerOneUpKey", "PlayerOneDownKey", "PlayerTwoUpKey", "PlayerTwoDownKey", "PauseKey" };
    15.    
    16.    
    17.         public void setPlayerOneUpKey()
    18.         {
    19.    
    20.             if (!waitingForInput)
    21.             {
    22.               waitingForInput = true;
    23.               temp = keys[0].text;
    24.               keys[0].SetText("PRESS ANY KEY");
    25.               keyToSave = "PlayerOneUpKey";
    26.             }
    27.         }
    28.    
    29.         public void setPlayerOneDownKey()
    30.         {
    31.            
    32.             if (!waitingForInput)
    33.             {
    34.                 waitingForInput = true;
    35.                 temp = keys[1].text;
    36.                 keys[1].SetText("PRESS ANY KEY");
    37.                 keyToSave = "PlayerOneDownKey";
    38.             }
    39.         }
    40.    
    41.         public void setPlayerTwoUpKey()
    42.         {
    43.            
    44.             if (!waitingForInput)
    45.             {
    46.                 waitingForInput = true;
    47.                 temp = keys[2].text;
    48.                 keys[2].SetText("PRESS ANY KEY");
    49.                 keyToSave = "PlayerTwoUpKey";
    50.             }
    51.         }
    52.    
    53.         public void setPlayerTwoDownKey()
    54.         {
    55.            
    56.             if (!waitingForInput)
    57.             {
    58.                 waitingForInput = true;
    59.                 temp = keys[3].text;
    60.                 keys[3].SetText("PRESS ANY KEY");
    61.                 keyToSave = "PlayerTwoDownKey";
    62.             }
    63.         }
    64.    
    65.         public void setPauseKey()
    66.         {
    67.            
    68.             if (!waitingForInput)
    69.             {
    70.                 waitingForInput = true;
    71.                 temp = keys[4].text;
    72.                 keys[4].SetText("PRESS ANY KEY");
    73.                 keyToSave = "PauseKey";
    74.             }
    75.         }
    76.    
    77.         public void cancelChanges()
    78.         {
    79.             waitingForInput = false;
    80.    
    81.             for (int i = 0; i < keys.Length; i++)
    82.             {
    83.                 if (keys[i].text.Equals("PRESS ANY KEY"))
    84.                 {
    85.                     keys[i].SetText(temp);
    86.                 }
    87.             }
    88.    
    89.         }
    90.    
    91.         public void setDefaults()
    92.         {
    93.             if (!waitingForInput)
    94.             {
    95.                 KeyCode[] defaultKeys = gameSettings.GetComponent<GameSettings>().defaultControls;
    96.                 for (int i = 0; i < keys.Length; i++)
    97.                 {
    98.                     keys[i].SetText(defaultKeys[i].ToString());
    99.                     gameSettings.GetComponent<GameSettings>().setControls(i, defaultKeys[i]);
    100.                     PlayerPrefs.SetInt(savedKeyNames[i], ((int)defaultKeys[i]));
    101.                 }
    102.             }
    103.         }
    104.    
    105.         void OnGUI()
    106.         {
    107.             e = Event.current;
    108.             if (e.isKey && waitingForInput)
    109.             {
    110.                 if (!e.keyCode.ToString().Equals("None"))
    111.                 {
    112.                     for (int i = 0; i < keys.Length; i ++)
    113.                     {
    114.                         if (keys[i].text.Equals("PRESS ANY KEY"))
    115.                         {
    116.                             if (!checkIfUsed(e.keyCode))
    117.                             {
    118.                                 keys[i].SetText(e.keyCode.ToString());
    119.                                 gameSettings.GetComponent<GameSettings>().setControls(i, e.keyCode);
    120.                                 PlayerPrefs.SetInt(keyToSave, (int)e.keyCode);
    121.                                 waitingForInput = false;
    122.                             }
    123.                             else
    124.                             {
    125.                                 audioManager.GetComponent<PlayAudio>().playInvalidSound();
    126.                                 keys[i].SetText("KEY IN USE");
    127.                                 StartCoroutine(ResetDuplicateKey(0.5f, keys[i]));
    128.                                 waitingForInput = false;
    129.                             }
    130.                         }
    131.    
    132.                     }
    133.                 }
    134.    
    135.             }
    136.         }
    137.    
    138.         bool checkIfUsed(KeyCode key)
    139.         {
    140.             for (int i = 0; i < gameSettings.GetComponent<GameSettings>().controls.Length; i++)
    141.             {
    142.                 if (key == gameSettings.GetComponent<GameSettings>().controls[i])
    143.                 {
    144.                     return true;
    145.                 }
    146.             }
    147.    
    148.             return false;
    149.         }
    150.    
    151.         void Start()
    152.         {
    153.             keys[0].SetText(gameSettings.GetComponent<GameSettings>().controls[0].ToString());
    154.             keys[1].SetText(gameSettings.GetComponent<GameSettings>().controls[1].ToString());
    155.             keys[2].SetText(gameSettings.GetComponent<GameSettings>().controls[2].ToString());
    156.             keys[3].SetText(gameSettings.GetComponent<GameSettings>().controls[3].ToString());
    157.             keys[4].SetText(gameSettings.GetComponent<GameSettings>().controls[4].ToString());
    158.         }
    159.    
    160.         IEnumerator ResetDuplicateKey(float time, TextMeshProUGUI keyText)
    161.         {
    162.             yield return new WaitForSeconds(time);
    163.             keyText.SetText(temp);
    164.         }


    Any suggestions on how I should force the game to use the text values of the TMPro objects that were set in the scripts and not in the editor?