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);
}
}