When I add a score, the scoreText just rapidly displays the scores added to itself
Hello, I am a newbie and I am making this android game that if the player has destroyed a meteor, a score is added to its score, well the problem is that I want to display the score in my scoreText, but whenever I initialize it in my Update(), it rapidly adds the score in my scoreText. I just cant figure out how to properly add the score to my scoreText This is my game manager script
public class GameManager : MonoBehaviour
{
public static int displayScores;
public int displayTheScore;
public Text scoreText;
// Start is called before the first frame update
void Start()
{
scoreText.text = "" + displayScores;
}
void Update(){
scoreText.text = "" + displayScores;
displayScores += Meteor.displayScore;
}
}
And this is the script to making the conditions that if the meteor is detroyed, a score is added to displayScore depending on the hits to the meteor
public class Meteor : MonoBehaviour
{
public int maxHealth;
public int currentHealth;
public float speed;
public int hits = 0;
public int score = 100;
public static int displayScore;
public int display;
public int currentHealthChecker;
public static int counter;
public Health healthBar;
public GameObject canvas;
public Transform effect;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.setMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
public void OnTriggerEnter2D(Collider2D other){
if(other.transform.tag == "bullet"){
hits++;
canvas.SetActive(true);
currentHealth--;
currentHealthChecker = currentHealth;
healthBar.setHealth(currentHealth);
display = displayScore;
if(currentHealth <= 0){
displayScore = score * hits;
Instantiate(effect, other.transform.position, other.transform.rotation);
Destroy(this.gameObject);
counter++;
canvas.SetActive(false);
}
Destroy(other.gameObject);
}
if(other.transform.tag == "bottom"){
Destroy(this.gameObject);
}
}
}
Answer by VolcanicMG · Jul 06, 2021 at 06:17 AM
It looks like your Update function in your game manager class is always adding up the score continuously every frame. I would add to the score once the meteor is destroyed within the meteor class so it only adds it up once instead.
that's what I did earlier, but I don't know why it wouldn't add up, maybe because the meteor is destroyed along with the script?
Yeah, make sure before the game object is destroyed you add the score up. I would generally never run anything after the game object is destroyed.
One other thing to keep in $$anonymous$$d. I would not have your scores static in the meteor game object. If you are making clones out of that class then you are going to have inflated score values since all the game objects will all share the same score counter. It would be best to put the static references in your game manager script and add them or whatever there. As for your counter, I would remove the static element from it too.
I removed the static from the counter, but I kept the static in the displayScore, because I needed the static so that I can call that variable outside of the class. I am now using IEnumerator for the scoring, but the score still isn't showing up.