Need help with enemy spawner

Hi guys,

Iv been trying to make a spawner for my enemies but iv hit a wall. At the moment I have a object (with a script) that deals with the spawning of the enemies and I have a script connected to the enemy object that makes it move towards the player. The problem is that when I destroy all the enemy objects, I cant spawn any more cuz there is no rigidbody to clone. I not sure what to do so any help would be very appreciated (still new to unity).

The following if the code that is in the enemy’s and the code in the spawner.

enemy’s code

using UnityEngine;
using System.Collections;

public class enemyscript : MonoBehaviour {
	
	float speed = 2.0f;	


	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () 
	{
		movement();
	}

	void movement()
	{
	transform.position = Vector3.MoveTowards(transform.position,GameObject.Find("player").transform.position,speed * Time.deltaTime);

	}
	

	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "Player" || other.gameObject.tag == "bullet")
		{
			Destroy(gameObject);
		}
	}
	
}

spawner code

using UnityEngine;
using System.Collections;

public class enemyspawnman : MonoBehaviour {
	
	public Rigidbody Enemyprefeb;

	float delay = 5.0f;

	float canspawn = 5;

	public float randx, randy, randz;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		enemyspawn ();
	
	}
	public void enemyspawn()
	{
		if (Time.time >= canspawn) 
		{
			randx = Random.Range(-7.0f,7.0f);
			randy = Random.Range(-3.0f,4.0f);
			randz = Random.Range(4.0f,5.0f);

		Enemyprefeb = Instantiate (Enemyprefeb, new Vector3(randx,randy,randz), transform.rotation) as Rigidbody;
			canspawn += delay;
		}

		
	}
	
}

When doing this spawning, use a true prefab, not a scene object. Prefabs are like blueprints, and don’t exist in the scene. More info:

http://docs.unity3d.com/Documentation/Manual/Prefabs.html

Then in your scene, you don’t assign the results of your Instantiate to your prefab. So leave out the ‘Enemyprefab =’ on line 34.

Take the Enemy you are cloning currently and save it as a prefab

(Drag it from the Hierarchy to the Project tab)

Now your Enemy is saved as a prefab.

In your script change your public rigidbody to a public GameObject.

In the editor drag the prefab (From the Project View) into the public GameObject slot in the Inspector Tab

Your spawner will now clone the prefab instead of a preexisting GameObject in the scene

Here is a copy of a spawner script I use, be aware of the disclaimer at the top of the code.

/*
 * This confidential and proprietary software may be used only as
 * authorised by a licensing agreement from ARM Limited
 * (C) COPYRIGHT 2014 ARM Limited
 * ALL RIGHTS RESERVED
 * The entire notice above must be reproduced on all authorised
 * copies and copies may only be made to the extent permitted
 * by a licensing agreement from ARM Limited.
 */

using UnityEngine;
using System.Collections;

public class SpawnerScript : MonoBehaviour 
{
	//Public Variables
    public GameObject prefabToSpawn;
    public float spawnTime;
    public float spawnTimeRandom;
	//Private Variables
    private float spawnTimer;

	//Used for initialisation
	void Start () 
    {
        ResetSpawnTimer();
	}
	
	//Update is called once per frame
	void Update () 
    {
	    spawnTimer -= Time.deltaTime;
	    if (spawnTimer <= 0.0f)
	    {
	        Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
	        ResetSpawnTimer();
	    }
	}

	//Resets the spawn timer with a random offset
    void ResetSpawnTimer()
    {
        spawnTimer = (float)(spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0);
    }
}

I hope this helps

P.S : A little tip for the future, avoid using .tag, use CompareTag() instead, ontop of that avoid FindObject or FindObjects, this is really inefficient especially when done on an Update. Try and define as much as possible using the inspector and public variables to avoid searching through every object in your scene unnecessarily