- Home /
How do I link a TMpro to a variable in another scene?
I'm basically having trouble telling the game to show the player gold in the UI. I have a TMPro in a scene, and a Manager that stores the gold and is in a DDOL scene. The thing I tried to do and WORKED was this in the coin object script:
public int amount;
public TextMeshProUGUI text;
and
Manager.Instance.UpdateGold(amount, text);
Destroy(gameObject);
And this in the manager:
public void UpdateGold(int amount, TextMeshProUGUI text)
{
playerGold += amount;
text.text = playerGold.ToString();
}
This works but I get the feeling that telling the manager what the text object is EVERY time isn't practical, you tell me if Im wrong. So I tried to make a new script that runs after the scene loads that stores the text object and links it to the manager gold variable:
public class UILinks {
public TextMeshProUGUI goldText;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
private void SetGold()
{
goldText = GameObject.FindGameObjectWithTag("Gold").GetComponent<TextMeshProUGUI>();
}
}
When I go back to Unity it tells me that the TextMeshProUGUI is never assigned and it will be its default null. What am I doing wrong? Also is there a better way for linking the UI objects than using FindGameObjectWithTag? Thanks.
TL:DR: How do I link a TMProUGUI with a variable in another scene at the start of the game scene?
Your answer
![](https://koobas.hobune.stream/wayback/20220612174504im_/https://answers.unity.com/themes/thub/images/avi.jpg)