Reputation: 11
I've made a simple relfex-dodging game in Unity in which a TextMeshPro text counts up when an obstacle passes by the player. The obstacle drops from above, falls down and gets destroyed when entering the trigger of a Box-collider. I've attached a Picture of the Scene view.
The "scoreText" variable in the example below is a reference to the TextMeshProUGUI component. When I run the game in Unity (with Unity Remote and my android phone as an input device), the score mechanic works as expected and the text property of the TextMeshProUGUI component changes from 0 to 1, from 1 to 2, and so on. When i hit "build and run" or just "build" in the Build Settings and export the apk-file, the game works well, but the text counts up to 1 and then remains like that. The obstacles are being destroyed, so the else if- block is being executed, too. Nothing changes from the Unity-version to Android, the code stays the same. I tried it on multiple devices, but the Problem stays. There are no errors, the app doesn't crash and I don't know what to do! I would very appreciate if you could help me! Here's the Code:
private void OnTriggerEnter2D(Collider2D other)
{
if (int.Parse(scoreText.text) == 0)
{
scoreText.SetText("1");
}
else if (int.Parse(scoreText.text) > 0)
{
int oldNumber = int.Parse(scoreText.text);
int newNumber = oldNumber + 1;
scoreText.SetText(newNumber.ToString());
}
Destroy(gameObject);
}
Upvotes: 0
Views: 839
Reputation: 11
Have you tried using scoreText.text = newNumber.ToString()? I personally never use SetText
Upvotes: 0