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 zviaz · Apr 11, 2015 at 04:01 AM · spawning-enemies

Cancel Spawn without Ending Round/Wave

So I've been working with one of the Unity tutorials for a while and have moved on to making additional modifications/core game modules.

I have a wave spawn timer that (for wave 1) lasts/spawns enemies for 30 seconds. Currently my logic is as follows:

Has it been longer than 30 seconds? If so, stop spawning enemies and load the next scene (Wave 2)

At first I thought I could just use invoke to delay the time between stopping the spawns and loading the next level but I encountered a problem. The next round will load even if there are enemies still in the current scene (which isn't a major problem but I would like to force the player to kill all enemies before being able to move on)

 using UnityEngine;
 
 public class EnemyManager : MonoBehaviour
 {
     public PlayerHealth playerHealth;
     public GameObject enemy;
     public float spawnTime = 3f;
     public Transform[] spawnPoints;
     private float timer;
     
     void Start ()
     {
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
         timer = Time.time + 30;
     }
     
     void Spawn ()
     {
         if(playerHealth.currentHealth <= 0f) 
         {
             return;
         }
 
         if (timer < Time.time) {
             CancelInvoke();
             Application.LoadLevel(1);
         }
             
         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
 
         Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
     }
 }


What I would like to do logically is the following:

Has it been longer than 30 seconds? If so, stop spawning enemies. Are there any active enemy gameobjects in the scene? if so do nothing. If there are not then load the next wave.

How do I check to see if there are any active enemy gameobjects in the current scene?

Any help is appreciated as it doesn't seem to be the easiest thing to solve.

Comment
Add comment · Show 1
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 zviaz · Apr 10, 2015 at 08:45 PM 0
Share

I'm aware I might be able to use GameObject.SetActive to set each enemy gameobject as active then use GameObject.activeInHierarchy to somehow check if any of the gameobjects are still active before loading the next wave but I'm struggling with how to put it together.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by AlwaysSunny · Apr 11, 2015 at 03:59 AM

The root of this question is about scene and reference management. It's a question we get half a dozen times a day it seems. I wonder if there's not enough emphasis on this topic in Unity lessons here.

Reference and scene management are important game design fundamentals, but the topic isn't exactly simple. There are alternate styles and preferences, but generally the most convenient way of tracking references wins.

Sometimes the script responsible for spawning an object can keep a collection of those spawned instances. (e.g. the level is cleared if the time is up and the list is empty).

Eventually in a more complicated project, you'll want to explore other more sophisticated ways of keeping track of what's going on in your game.

Comment
Add comment · Show 1 · 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 zviaz · Apr 15, 2015 at 06:47 PM 0
Share

The lessons provided by Unity (and external sources) seem to gloss over management of scene objects and I have not found a decent reference on how to manage multiple objects (especially multiple enemy "types")

I ended up using the score counter to do it. if wave 1 contains 10 enemies, and killing ten enemies gives you a score of 1000, then I just check the players score, if not 1000 then do nothing, if 1000 then load wave 2. This seems like a very "hackish" way to do it but until I can find a tutorial or video that shows me otherwise I can't do much more.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Want to spwan obstacle from top to down in a 2D top to down game. 0 Answers

Spawn multiple enemies in relation to gameobject [SOLVED] 1 Answer

(2D) Spawn enemies off-screen and have them to follow the Player as a target 0 Answers

SpawnEnemies script slows the game significantly after just 40 or so? 1 Answer

Spawn Point 2 Answers


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