- Home /
When objects wihtin a group are dead, I win a bonus
Hi guys,
In my game I have some objects, and each object has a life. When I kill these objects, I earn points. What I want to do now is to give the player a bonus for killing a group of objects. So, I want to group five objects, for example, and when all the objects are dead, I get a bonus.
My game's scoring system is divided into two scripts. A part is attached to the player, and gives points when he collides with objects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MonstroScript : MonoBehaviour
{
public float damage;
public static int score;
public Text scoreText;
// Start is called before the first frame update
void Start()
{
score = 0;
}
void OnTriggerEnter(Collider other)
{
other.gameObject.GetComponent<PrediosScript> ().TakeDamage (damage) ;
AddScore();
}
void AddScore()
{
score++;
scoreText.text = "Pontuação: " + score.ToString();
}
}
The other part is linked to the objects that the player attacks. When these objects die, they give you a bonus. I linked the bonus to the objects because I want to create objects with different bonus values.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrediosScript : MonoBehaviour
{
public float health;
public float currentHealth;
public bool alive = true;
public int bonusScore;
public GameObject inicialState;
public GameObject destroyed;
// Start is called before the first frame update
void Start()
{
alive = true;
currentHealth = health;
}
public void TakeDamage(float damageIntensity)
{
currentHealth -= damageIntensity;
if (!alive)
{
return;
}
if (currentHealth <= 0)
{
currentHealth = 0;
alive = false;
gameObject.SetActive(false);
MonstroScript.score = MonstroScript.score + bonusScore;
Replace(inicialState, destroyed);
}
}
void Replace(GameObject state1, GameObject state2) {
Instantiate(state2, state1.transform.position, Quaternion.identity);
Destroy(state1);
}
}
The objects have two states, one alive and other dead. So i was thinking do something like "if my group have only dead objects, the bonus is accounted"