I’m making a time trial like game, where you have to destroy targets as fast as possible. Your time is recored and displayed in the next scene. I have been able to apply this to each target; however, when one target is destroyed it’ll immeduiately change scenes without the others being destroyed.
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class Target : MonoBehaviour
{
public float health = 1f;
public float score = 0f;
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
score++;
}
}
void Die()
{
Destroy(gameObject);
}
private void OnDestroy()
{
SceneManager.LoadScene("FinalTime");
}
}
Good morning mate =) .I found a simple way to do that .First of all I created a array gameobject named as enemies than I equaled it to GameObject.FindGameObjectWithTag(“Enemy”); .After that I created a new for function which checks every enemy if for function founds every enemy dead it will enable bool value. Here the code =)
public class Logic : MonoBehaviour
{
public bool isAllEnemiesDied;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject[] enemies= GameObject.FindGameObjectsWithTag("Enemy");
if (enemies.Length == 0&&isAllEnemiesDied==true)
{
Debug.Log("You killed all enemies wp=)");
}
for(int i = 0; i < enemies.Length; i++)
{
if (enemies *!= null&&isAllEnemiesDied==false)*
{
isAllEnemiesDied = false;
// Debug.Log(“There are a " + enemies.Length + " enemy alive =(”);
}
}
}
}
When I writing here I notice that we can make this more shorter =). In update function these two lines will be do same think I guess =)
GameObject[] enemies= GameObject.FindGameObjectsWithTag(“Enemy”);
if (enemies.Length == 0&&isAllEnemiesDied==true)
{
Debug.Log(“You killed all enemies wp=)”);
}