- Home /
How to destroy an enemy spawner in a turned base RPG?
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);
}
}
Answer by MikeNewall · Mar 29, 2021 at 08:20 PM
Option 1. You could use a static variable to toggle the state of the battle spawner from outside of the town scene.
https://learn.unity.com/tutorial/statics-l?signup=true
Option 2. Create an object to hold the state of the spawner which persists between scenes
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Option 3. Create a scriptable object to hold the state of the spawner.
https://docs.unity3d.com/ScriptReference/ScriptableObject.html
Your answer
Follow this Question
Related Questions
Is it possible to destroy an object only within the collider? 1 Answer
How can I destroy an object if it touches NOTHING 1 Answer
Enemies spawning close/ on top of the Player. 1 Answer
how retrieve final player position in previous scene after switching from a new scene? in 2d rpg 4 Answers
Enemy Spawner help 1 Answer