Why does my Gem counter not work? (screenshot included for explanation / easy code)
Hey everyone,
I have four UI text on my canvas:
blueGemText
greenGemText
redGemText
diamondGemText
Now, I have this following script attached to each of the Text elements.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GemCounter : MonoBehaviour {
static Text scoreText;
//bools
public bool blueGem = false;
public bool greenGem = false;
public bool redGem = false;
public bool diamondGem = false;
void Start () {
scoreText = gameObject.GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
if (blueGem) {
scoreText.text = Score.blueGems.ToString ();
}
if (greenGem) {
scoreText.text = Score.greenGems.ToString ();
}
if (redGem) {
scoreText.text = Score.redGems.ToString ();
}
if (diamondGem) {
scoreText.text = Score.diamondGems.ToString ();
}
}
}
On each UI Text I set the correct boolean. For example on the blueGemText I checked the 'blueGem' boolean set to true. Now when I run the game only the blue gem counter gets updated every time I pick up a blue gem. The green, red, and diamond counter all stay the same. (However, when I die, and the scene resets, it suddenly starts counting the green diamonds and the rest stays the same)
I already also tried using 'else if' statements and using tags, but that didn't work either.
What am I doing wrong here?
Thanks in advance -- Nathan.
Including screenshot for reference:
being that i don't use C# i was thinking it could be that you don't have a GUI method started for updating the numbers.
Answer by DiegoSLTS · May 31, 2016 at 01:02 AM
static Text scoreText;
Remove that "static" keyword. All the scripts are overwriting the same variable with it's own reference to a text component, and after that they all update the last one.