Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zandlie · Aug 28, 2016 at 10:44 AM · c#destroyspawndestroy objectenemy spawn

Enemy Spawner help

Hello, just to start out i have a spawner that when it turns on, will spawn my creatures for me. but when it turns off through a day and night cycle, when the time comes to turn it on again it wont instantiate the gameobject selected. so the problem i am having is that it will loop the spawn enemy once but when it goes off then back on it wont. here are all the C# codes i have affiliated with my spawner.

this is my spawner

 using UnityEngine;
 using System.Collections;
 
 public class EnemyController : MonoBehaviour {
     
     public GameObject enemy;
     public Vector3 spawnValues;
     public float spawnWait;
     public float startWait;
     
 
     public static EnemyController enemycontroller;
 
     void Awake(){
         enemycontroller = this;
     }
     
     void Start(){
         StartCoroutine (SpawnWaves ());
     }
 
     void Update(){
         SpawnWaves ();
     }
 
     IEnumerator SpawnWaves(){
         yield return new WaitForSeconds(startWait);
         while (true) {
             Vector3 spawnPosition = new  Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (enemy, spawnPosition, spawnRotation);
             Debug.Log ("Enemy Spawned!");
             yield return new WaitForSeconds (spawnWait);
         }
     }
 }
 

this is my day night cycle code

 using UnityEngine;
 using System.Collections;
 
 public class IslandPicker : MonoBehaviour {
 
     public GameObject[] Island;
     public GameObject Spawners;
     public int CurrentIsland;
 
 
 
 
     void Start () {
         for (var i = 0; i < Island.Length; i++) {
             Island [i].SetActive (false);
         }
         Spawners.SetActive (false);
     }
     
     void Update () {
 
     }
     void OnTriggerEnter(Collider col){
         if (col.tag == "NewDay") {
             NewIsland ();
             Debug.Log ("NewDay");
         }
         if (col.tag == "Night") {
             Spawners.SetActive (true);
             Debug.Log ("Night Has Come!");
         }
         if (col.tag == "Day") {
             Spawners.SetActive (false);
             Debug.Log ("Day Has Come!");
             DestroyAll ("Wraith");
         }
 
     }
     void NewIsland(){
         int newIsland = Random.Range (0, Island.Length);
         //Island [CurrentIsland].SetActive (false);
         CurrentIsland = newIsland;
         Island [CurrentIsland].SetActive (true);
     }
     void DestroyAll(string tag){
         GameObject[] ObjectsToDestroy = GameObject.FindGameObjectsWithTag ("Wraith");
         foreach (GameObject DestroyObject in ObjectsToDestroy) {
             Destroy (DestroyObject);
         }
     }
 }
 

i am not that perfect at coding so forgive the unorganized code :)

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
3
Best Answer

Answer by DiegoSLTS · Aug 28, 2016 at 04:22 PM

You have a few problems there:

  • In your Update your calling SpawnWaves like it's a normal method, but Coroutines don't work like that in C#. If you defined a coroutine you can call it like a normal function but it won't run like a coroutine, it'll ignore all the waits and yields and just return at the first return. You have to use StartCoroutine, like you did on Start.

  • Calling or starting a coroutine in every update is a really bad idea, remove that, don't use Update to start coroutines unless you're sure the "StartCoroutine" will run only once during the coroutine duration. And even if you do that, it's usually a sign of bad architecture on your code.

  • When you call SetActive on a gameObject, every script attached to it will execute the OnEnable (for SetActive(true)) and OnDisable (for SetActive(false)) methods. You should start the coroutine in OnEnable if you want to turn the spawner on an off by deactivating the spawner. Relying on Update to start/stop something is a bad idea like I said in the previous point. There are cleaner methods though, like adding methods that start and stop the coroutine with a descriptive name and calling those methods, instead of activating and deactivating the entire gameObject. You can stop a coroutine if you store a reference to it. StartCoroutine returns a coroutine, that you can pass later to StopCoroutine to stop it.

  • You're starting the coroutine in the Start method of EnemyController and deactivating the Spawner in the Start method of IslandPicker. Starting a coroutine and deactivating the object at the same time makes little sense.

I think something like this should work:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyController : MonoBehaviour {
  
     public GameObject enemy;
     public Vector3 spawnValues;
     public float spawnWait;
     public float startWait;
     private Coroutine spawnCoroutine;
 
     public static EnemyController enemycontroller;
 
     void Awake(){
         enemycontroller = this;
     }

     public void StartSpawnWaves() {
         if (spawnCoroutine != null)
             Debug.LogWarning("Spawner already running");
         else
             spawnCoroutine = StartCorouitne(SpawnWaves());
     }

     public void StopSpawnWaves() {
         if (spawnCoroutine != null) {
             StopCoroutine(spawnCoroutine);
             spawnCoroutine = null;
         }
     }

     IEnumerator SpawnWaves(){
         yield return new WaitForSeconds(startWait);
         while (true) {
             Vector3 spawnPosition = new  Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range (-spawnValues.z, spawnValues.z));
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (enemy, spawnPosition, spawnRotation);
             Debug.Log ("Enemy Spawned!");
             yield return new WaitForSeconds (spawnWait);
         }
     }
 }

And in your day/night script...

 using UnityEngine;
 using System.Collections;
 
 public class IslandPicker : MonoBehaviour {
 
     public GameObject[] Island;
     public EnemyController Spawner; //a reference to the spawner script instead of the GameObject
     public int CurrentIsland;

     void Start () {
         for (var i = 0; i < Island.Length; i++) {
             Island [i].SetActive (false);
         }
     }
  
     void OnTriggerEnter(Collider col){
         ...
         if (col.tag == "Night") {
             Spawners.StartSpawnWave();
             Debug.Log ("Night Has Come!");
          }
          if (col.tag == "Day") {
              Spawners.StopSpawnWave();
              Debug.Log ("Day Has Come!");
              DestroyAll ("Wraith");
          }
     }

     ...

 }
Comment
Add comment · Show 2 · 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
avatar image Zandlie · Aug 28, 2016 at 07:01 PM 0
Share

Thank you so very much! i was struggling with this for awhile now. Upvoted and checked answered! though i do have one question. can this work with an array? say i have spawners on my islands and when a island turns on is it possible for it to work the same way? i just have to call it right?

avatar image DiegoSLTS Zandlie · Aug 28, 2016 at 11:06 PM 0
Share

I'm not sure what you mean by "can this work with an array"... An array of EnemyController? Well, yes, just change the "Spawners" type to be an array (EnemyController[]), drop the spawners in the inspector like always and ins$$anonymous$$d of:

 Spawners.StartSpawnWave();

do:

 foreach (EnemyController spawner in Spawners)
     spawner.StartSpawnWave();

The same for StopSpawnWave.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Respawning Single Enemy at a Random Range 1 Answer

How do I restart enemy spawn when boss is Destroyed in Unity C# version 5.2 1 Answer

how to destroy a object only in game and not the prefab 2 Answers

how to destroy enemy 1 Answer

Multiple Cars not working 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