- Home /
Score text is not updating on collision
Hello,
I have multiple prefabs in a scene with two tags called Square and Player. they have a boxCollider2d attached to them.
these prefabs has a health point and i decrease it when the collision occurs. then I check if a health point for any object is 0, then score +1 and so on...
Now, the problem is that is updating only, when a gameObject tagged as Player health point is reached to zero. What is the problem with this script?
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
public class Smash : MonoBehaviour
{
public int hitPoints;
public Text score;
public Text highScore;
public int mc = 0;
// Use this for initialization
void Start()
{
highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}
// Update is called once per frame
void Update()
{
}
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Square" || collision.gameObject.tag == "Player")
{
hitPoints--;
var getText = gameObject.GetComponentInChildren<TextMesh>();
getText.text = hitPoints.ToString();
if (hitPoints == 0)
{
mc++;
StartCoroutine(BrokenSqaures());
}
if (mc > PlayerPrefs.GetInt("HighScore", 0))
{
PlayerPrefs.SetInt("HighScore", mc);
highScore.text = mc.ToString();
}
}
}
private IEnumerator BrokenSqaures()
{
score.text = mc.ToString();
yield return new WaitForSeconds(particle.main.startLifetime.constantMax);
Destroy(gameObject);
}
}
Answer by Llama_w_2Ls · Jan 20, 2021 at 10:12 PM
The issue is here:
if (hitPoints == 0)
{
mc++;
StartCoroutine(BrokenSqaures());
}
You only start the coroutine called BrokenSquares, which sets your score text to mc.ToString() when your health is zero. I recommend you just put this line score.text = mc.ToString(); outside the coroutine and into the collision function, so it is called whenever you change the text. @Hefaz
but i use some other things as well inside the coroutine and I need the text only to be updated whenever the health is zero. @Llama_w_2Ls
for example here public void OnCollisionEnter2D(Collision2D collision) {
if (collision.collider.gameObject.tag == "Square" || collision.collider.gameObject.tag == "Player")
{
mc++;
score.text = mc.ToString();
}
}
the text is updating only if it hits the object Tagged as Player and not updating when two objects with the Square tag are collided.
Have you checked as to whether the square objects ever collide? Try putting a Debug.Log in there to see whether the collision function is called correctly. $$anonymous$$aybe you're missing a rigidbody or collider.
Also, you should use if (collision.collider.gameObject.CompareTag("Square") ...) It's a bit more efficient, according to Unity $$anonymous$$essages.
Yeah, collision is working fine. I will make the question clearer.
How do you update a score text, when a health point of an object has been reached to 0, if there are multiple collissions. each collision decrease one health point.
something like this.

There should be a script on each square that handles collisions. It should look something like this:
public class Square : $$anonymous$$onoBehaviour
{
public int health;
public Text Score;
void OnCollisionEnter2D(Collider2D col)
{
if (col.collider.gameObject.CompareTag("Square"))
{
Square square = col.collider.gameObject.GetComponent<Square>();
square.health -= 1;
this.health -= 1;
}
}
void Update()
{
if (this.health == 0)
{
Score.text = (int.Parse(Score.text) + 1).ToString();
}
}
}
Your answer