Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lefk36 · Mar 16, 2021 at 10:51 AM · 2d gamedestroy objectturn-basedenemy spawnrpg-game

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);
     }
 
     }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges