I have a town scene in my game, where the player collides with the battle spawner and then it changes to the battle scene. What I want is to destroy the battle spawner in the town scene when the battle scene has ended. Does anyone have any ideas?
This is the code for the Enemy Spawner:
public class EnemySpawner : MonoBehaviour
{
[SerializeField]
private GameObject enemyEncounterPrefab;
private GameObject dialogue;
public bool battleEnded = true;
private float dialogueSpeed = 100.0f;
private bool enemyDialogueActive;
private bool spawning = true;
public float Timer;
void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
dialogue = GameObject.Find("DialogueGoblins");
enemyDialogueActive = dialogue.GetComponent<DialogueManager>().dialogueActive;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
if (!enemyDialogueActive)
{
WaitforBattle();
}
}
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "Battle")
{
if (this.spawning)
{
Instantiate(enemyEncounterPrefab);
}
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}
void WaitforBattle()
{
enemyDialogueActive = false;
this.spawning = true;
SceneManager.LoadScene("Battle");
}
void OnTriggerExitExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Destroy(this.gameObject);
}
}
}
This is the code for the turn system:
public class TurnSystem : MonoBehaviour {
private List<UnitStats> unitsStats;
private GameObject playerParty;
public GameObject enemyEncounter;
private GameObject goblinSpawner;
[SerializeField]
private GameObject actionsMenu, enemyUnitsMenu;
void Start() {
this.playerParty = GameObject.Find ("PlayerParty");
this.goblinSpawner = GameObject.Find("GoblinSpawner");
unitsStats = new List<UnitStats> ();
GameObject[] playerUnits = GameObject.FindGameObjectsWithTag("PlayerUnit");
foreach (GameObject playerUnit in playerUnits) {
UnitStats currentUnitStats = playerUnit.GetComponent<UnitStats> ();
currentUnitStats.calculateNextActTurn (0);
unitsStats.Add (currentUnitStats);
}
GameObject[] enemyUnits = GameObject.FindGameObjectsWithTag("EnemyUnit");
foreach (GameObject enemyUnit in enemyUnits) {
UnitStats currentUnitStats = enemyUnit.GetComponent<UnitStats> ();
currentUnitStats.calculateNextActTurn (0);
unitsStats.Add (currentUnitStats);
}
unitsStats.Sort ();
this.actionsMenu.SetActive (false);
this.enemyUnitsMenu.SetActive (false);
this.nextTurn ();
}
public void nextTurn() {
GameObject[] remainingEnemyUnits = GameObject.FindGameObjectsWithTag ("EnemyUnit");
if (remainingEnemyUnits.Length == 0 )
{
this.enemyEncounter.GetComponent<CollectReward>().collectReward();
SceneManager.LoadScene("Town");
this.destroySpawner();
}
GameObject[] remainingPlayerUnits = GameObject.FindGameObjectsWithTag ("PlayerUnit");
if (remainingPlayerUnits.Length == 0) {
SceneManager.LoadScene("Title");
}
UnitStats currentUnitStats = unitsStats [0];
unitsStats.Remove (currentUnitStats);
if (!currentUnitStats.isDead()) {
GameObject currentUnit = currentUnitStats.gameObject;
currentUnitStats.calculateNextActTurn (currentUnitStats.nextActTurn);
unitsStats.Add (currentUnitStats);
unitsStats.Sort ();
if (currentUnit.tag == "PlayerUnit") {
this.playerParty.GetComponent<SelectUnit> ().selectCurrentUnit (currentUnit.gameObject);
} else {
currentUnit.GetComponent<EnemyUnitAction>().act ();
}
} else {
this.nextTurn ();
}
}
public void WaitThenNextTurn() {
StartCoroutine(WaitThenNextTurnRoutine());
}
private IEnumerator WaitThenNextTurnRoutine() {
yield return new WaitForSeconds(1.5f);
this.nextTurn();
}
public void destroySpawner()
{
this.goblinSpawner.SetActive(false);
}
}