• 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 bradh97 · Jun 21, 2015 at 06:04 PM · spawnspawning

Spawn a number of enemy's on the map

Hello I am making a zombie survival game and I want to Spawn a number of enemy's on the map can you help here is my code I am using unity 5 thank you.

 using UnityEngine;
 using System.Collections;
 
 public class GameManagement : MonoBehaviour {
     public int round = 1;
     int zombesInRound = 5;
     int zombiesSpawnInRound = 0;
     float zombieSpawnTimer = 0;
     public Transform[] zombieSpawnPoints;
     public GameObject zombieEnemy;
 
     void Start () {
     
     }
     
 
     void Update () {
         if (zombiesSpawnInRound < zombesInRound)
         {
             if(zombieSpawnTimer > 30)
             {
                 SpawnZombie ();
                 zombieSpawnTimer = 0;
             }
             else
             {
                 zombieSpawnTimer++;
             }
         }
     }
     void SpawnZombie()
     {
         Vector3 randomSpawnPiont = zombieSpawnPoints[Random.Range (0, zombieSpawnPoints.Length)].position;
         Instantiate(zombieEnemy,randomSpawnPiont,Quaternion.identity);
         zombesInRound ++;
     }
 }
 
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 GregoryNeal · Jun 21, 2015 at 06:28 PM 0
Share

First of all you shouldn't be incrementing zombieSpawnTimer per frame if you are using it as a timer. If you want an actual timer you are much better off reading up on Time.deltaTime.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jun 21, 2015 at 07:23 PM

You might consider using two variables for your zombie spawn rate.

 public float zombieSpawnRate = 5.0f; // One every X seconds
 float zombieSpawnTimer;
 
 void Start()
 {
     zombieSpawnTimer = zombieSpawnRate;
 }
 
 void Update()
 {
     if(zombiesSpawnInRound < zombiesInRound)
     {
         if(zombieSpawnTimer <= 0)
         {
             SpawnZombie();
             zombieSpawnTimer = zombieSpawnRate;
         }
         // If used here, timer will wait at X seconds
         // before a new zombie can spawn when at the
         // maximum possible in the round
         zombieSpawnTimer -= Time.deltaTime;
     }
     // If used here, zombie can spawn
     // instantly upon another's death when
     // at the maximum in the round
     // zombieSpawnTimer -= Time.deltaTime;
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Different way to spawn prefabs 0 Answers

Spawning in Specific Areas 1 Answer

Enter/exit buildings spawn 2 Answers

How to spawn all humans at once and activate then with time? 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges