- Home /
How to spawn enemy? (Enemies)
Hi, I've been trying to write a script for my (ZombieStyle) FPS so when the game first loads, several "enemies" will be spawned at random spawn points I have set throughout my map. (I used empty GameObjects) I want a lot of zombies walking around aimless (Have already written the AI all works fine) I just want it to be completely random so while playing I don't learn where I will find them. I want it random.
Also if I start killing zombies to quickly then there wont be any left and thats boring. So how would I make it to where if there are less that however many zombies, Instantiate however many(Random.Range maybe). Just something to keep a decent amount of zombies on the street. Sorry to ask for a script, but I've been searching for the past 4 hours and trying with the API and can't seem to make anything work.
Here is what I have so far, I have no clue what I am doing so I started it off as spawning just 1 prefab. I can't figure out how to make Instantiate work, it says it requires 4 arguements or something. If you can write it to spawn alot when the game starts that would help.
PS: My game is all in C#
using UnityEngine;
using System.Collections;
/// <summary>
/// This is the EnemySpawn script.
/// This script is attached to the SpawnManager(EmptyGameObject).
/// </summary>
public class EnemySpawn : MonoBehaviour {
//--------------Variables Start-----------------
private GameObject[] enemySpawnPoints;
private int enemySpawnGroup = 0;
public Transform enemyAI;
public bool iAmAnEnemy = false;
//--------------Variables End------------------
void Awake()
{
//Call the SpawnEnemy function.
SpawnEnemy();
}
// Use this for initialization.
void Start () {
}
// Update is called once per frame.
void Update () {
}
void SpawnEnemy ()
{
//Find all of the enemy spawn points and place a reference to them in the array
//enemySpawnPoints.
enemySpawnPoints = GameObject.FindGameObjectsWithTag("SpawnEnemy");
//Randomly select one of those spawn points.
GameObject randomEnemySpawn = enemySpawnPoints[Random.Range(0, enemySpawnPoints.Length)];
//Instantiate the enemy at the randomly selected spawn point.
Instantiate(enemyAI, randomEnemySpawn.transform.position,
randomEnemySpawn.transform.rotation, enemySpawnGroup);
}
}
Sorry about the variables, For some reason after i hit "Ask question" they went all out of order.
it's possible this very long post could help you
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Answer by Maulik2208 · Dec 17, 2012 at 01:39 PM
Why don't you try a timer let say timer of 1 min and after every 1 min start instantiating the Zombies..... Not sure but this could be a way you can have a good amount of enemy to kill..........for timer you can use Enumarator and wait for second and you are all set to go......Cheers Enjoy.......Don't forget to tick if found useful
Your answer
![](https://koobas.hobune.stream/wayback/20220613085538im_/https://answers.unity.com/themes/thub/images/avi.jpg)