Hello everyone,
I seem to be having trouble having a level clear or AllClear() method call after all Generators have been destroyed, or SetActive(false). I tried a bool and have then see if they are all destroyed to have the AllClear() method come up but it doesn’t really work the way I intended any help would be appreciated.
/////////////////////////Most of the heavy lifting is done here/////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoDamage : MonoBehaviour {
[Header("Updated Generator Health")]
public int damageToGive = 50;
public int generatorAmountCount = 3;
public bool allGeneratorsDestroyed = false;
public GameManager gameManager;
[Header("Generator Sound And ParticleEffects")]
public AudioSource explosion;
public Transform explosionSpawnPoint;
public GameObject explosionMobilePrefab;
// Use this for initialization
void Start ()
{
if (gameManager == null)
{
gameManager = GetComponent<GameManager>();
}
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
allGeneratorsDestroyed = false;
if (other.tag == "generator" || other.tag == "generator2" || other.tag == "generator3")
{
other.GetComponent<HealthControllerGenerator>().TakeDamage(damageToGive);
Instantiate(explosionMobilePrefab, explosionSpawnPoint.position, explosionSpawnPoint.rotation);
explosion.Play();
if (other.GetComponent<HealthControllerGenerator>().currentHealth ==0)
{
allGeneratorsDestroyed = false;
// experimenting and turing this off for now
//generatorAmountCount -= 1; //Remove the amount
//so far turning off each individual generator based on tag if currenthealth is ==0
if (other.tag == "generator") // && GetComponent<HealthControllerGenerator>().currentHealth == 0) // previous implemented code wasn't working.
{
generatorAmountCount -= 1; //Remove the amount
other.gameObject.SetActive(false);
gameManager.StartCoroutine("Generator1Destroyed");
}
if (other.tag == "generator2" )
{
generatorAmountCount -= 1; //Remove the amount
other.gameObject.SetActive(false);
}
if (other.tag == "generator3")
{
generatorAmountCount -= 1; //Remove the amount
other.gameObject.SetActive(false);
// check to see if generator1 and generator2 are destroyed. Then making sure if 3rd generator is destroyed before AllClear() is called.
// This is working woohoo!
if (GameObject.FindGameObjectWithTag("generator") == false && GameObject.FindGameObjectWithTag("generator2") == false)
{
gameManager.Level1Finished();
}
}
}
}
}
}
//////////////////////This is the method called for damage removal but is used in the main DoDamage./////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthControllerGenerator : MonoBehaviour {
[Header("Generator Settings")]
public int currentHealth = 200;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void TakeDamage(int damageToTake)
{
currentHealth -= damageToTake;
}
}