Problem with TextMesh Pro when running a build of the game - how to get the right text?
Hello, 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:
using System.Collections;
using UnityEngine;
using TMPro;
public class ChangeKey : MonoBehaviour
{
public TextMeshProUGUI[] keys = new TextMeshProUGUI[5];
private bool waitingForInput = false;
private string temp;
private Event e;
public GameObject gameSettings;
public GameObject audioManager;
private string keyToSave;
private string[] savedKeyNames = { "PlayerOneUpKey", "PlayerOneDownKey", "PlayerTwoUpKey", "PlayerTwoDownKey", "PauseKey" };
public void setPlayerOneUpKey()
{
if (!waitingForInput)
{
waitingForInput = true;
temp = keys[0].text;
keys[0].SetText("PRESS ANY KEY");
keyToSave = "PlayerOneUpKey";
}
}
public void setPlayerOneDownKey()
{
if (!waitingForInput)
{
waitingForInput = true;
temp = keys[1].text;
keys[1].SetText("PRESS ANY KEY");
keyToSave = "PlayerOneDownKey";
}
}
public void setPlayerTwoUpKey()
{
if (!waitingForInput)
{
waitingForInput = true;
temp = keys[2].text;
keys[2].SetText("PRESS ANY KEY");
keyToSave = "PlayerTwoUpKey";
}
}
public void setPlayerTwoDownKey()
{
if (!waitingForInput)
{
waitingForInput = true;
temp = keys[3].text;
keys[3].SetText("PRESS ANY KEY");
keyToSave = "PlayerTwoDownKey";
}
}
public void setPauseKey()
{
if (!waitingForInput)
{
waitingForInput = true;
temp = keys[4].text;
keys[4].SetText("PRESS ANY KEY");
keyToSave = "PauseKey";
}
}
public void cancelChanges()
{
waitingForInput = false;
for (int i = 0; i < keys.Length; i++)
{
if (keys[i].text.Equals("PRESS ANY KEY"))
{
keys[i].SetText(temp);
}
}
}
public void setDefaults()
{
if (!waitingForInput)
{
KeyCode[] defaultKeys = gameSettings.GetComponent<GameSettings>().defaultControls;
for (int i = 0; i < keys.Length; i++)
{
keys[i].SetText(defaultKeys[i].ToString());
gameSettings.GetComponent<GameSettings>().setControls(i, defaultKeys[i]);
PlayerPrefs.SetInt(savedKeyNames[i], ((int)defaultKeys[i]));
}
}
}
void OnGUI()
{
e = Event.current;
if (e.isKey && waitingForInput)
{
if (!e.keyCode.ToString().Equals("None"))
{
for (int i = 0; i < keys.Length; i ++)
{
if (keys[i].text.Equals("PRESS ANY KEY"))
{
if (!checkIfUsed(e.keyCode))
{
keys[i].SetText(e.keyCode.ToString());
gameSettings.GetComponent<GameSettings>().setControls(i, e.keyCode);
PlayerPrefs.SetInt(keyToSave, (int)e.keyCode);
waitingForInput = false;
}
else
{
audioManager.GetComponent<PlayAudio>().playInvalidSound();
keys[i].SetText("KEY IN USE");
StartCoroutine(ResetDuplicateKey(0.5f, keys[i]));
waitingForInput = false;
}
}
}
}
}
}
bool checkIfUsed(KeyCode key)
{
for (int i = 0; i < gameSettings.GetComponent<GameSettings>().controls.Length; i++)
{
if (key == gameSettings.GetComponent<GameSettings>().controls[i])
{
return true;
}
}
return false;
}
void Start()
{
keys[0].SetText(gameSettings.GetComponent<GameSettings>().controls[0].ToString());
keys[1].SetText(gameSettings.GetComponent<GameSettings>().controls[1].ToString());
keys[2].SetText(gameSettings.GetComponent<GameSettings>().controls[2].ToString());
keys[3].SetText(gameSettings.GetComponent<GameSettings>().controls[3].ToString());
keys[4].SetText(gameSettings.GetComponent<GameSettings>().controls[4].ToString());
}
IEnumerator ResetDuplicateKey(float time, TextMeshProUGUI keyText)
{
yield return new WaitForSeconds(time);
keyText.SetText(temp);
}
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?