Spawning Mobs

Rushil Reddy

Now add an empty game object to scene and position it at (25,0,-20). Call it zSpawn and next to its name in the inspector panel click on the multicolored box and choose the grey option so you can see the object in the scene. Now Add Component> New Script and name it Spawn.

Add Component>Text and change it to say “Score:” Change its transform values to match ours.

using UnityEngine;

using System.Collections;

public class Spawn : MonoBehaviour {

public GameObject zombiePrefab;

public Transform target;

public float interval = 3;

// Use this for initialization

void Start() {

InvokeRepeating("SpawnNext", interval, interval);


// Update is called once per frame

void SpawnNext() {

var x = transform.position;

var zRand = Random.Range(-34.0f, -3.0f);

x.z = zRand;

GameObject zombie = Instantiate(zombiePrefab, x, Quaternion.identity) as GameObject;

zombie.GetComponent().barrel = target;



Now drag the zombie prefab and the barrel game object into the zSpawns inspector tab(See Below).

Hit play and zombies should be spawning every 3 seconds and making their way over to you.