I have a script that I want to use to destroy a select objects with a specific tag when a score reaches the number zero. I know how to actually destroy the objects, but how would I put it so it works when something happens? I’m somewhat new at coding, and would like to learn more.
Here’s the script
public class UICollisionControl : MonoBehaviour
{
public bool destroyEnemy;
public Text scoreUI;
public int increaseScore;
public int decreaseScore;
public int score;
void Start()
{
audioSource = GetComponent<AudioSource>();
score = 64;
}
void Update(){
if(scoreUI != null){
scoreUI.text = score.ToString();
}
}
// only for GameObjects with a mesh, box, or other collider except for character controller and wheel colliders
void OnCollisionEnter(Collision other)
{
if(audioSource != null && !audioSource.isPlaying){
audioSource.PlayOneShot(collisionAudio, 0.5F);
}
if(destroyEnemy == true && other.gameObject.tag == "Enemy")
{
Destroy(other.gameObject);
}
if (scoreUI != null && decreaseScore != 0 && other.gameObject.tag == "Enemy")
{
score -= decreaseScore;
}
}
// only for GameObjects with a character controller applied
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
// if no rigidbody
if (body == null || body.isKinematic)
{
return;
}
// don't push ground or platform GameObjects below player
if (hit.moveDirection.y < -0.3)
{
return;
}
if(audioSource != null && !audioSource.isPlaying){
audioSource.PlayOneShot(collisionAudio, 0.5F);
}
if(destroyEnemy == true && hit.gameObject.tag == "Enemy")
{
Destroy(hit.gameObject);
}
if (scoreUI != null && decreaseScore != 0 && hit.gameObject.tag == "Enemy")
{
score -= decreaseScore;
}
}