- Home /
How to respawn enemies after one is destroyed?
So I am doing this simple arcade style shooter game. My problem is that I know once my laser hits the enemy object it should get destroyed but once it get destroyed it is gone forever. I want to know how can I have a continuous enemy objects come at my spaceship even if it is destroyed. Its a 2d game but when I set up my game I followed the official unity3d beginner space shooter tutorial. In that tutorial coroutine were used but I am doing mine a bit differently. Here is how my code looks
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
Transform myTransform;
public int minSpeed;
public int maxSpeed;
int x,y,z;
public int currentSpeed;
// Use this for initialization
void Start () {
x = -7;
y = 0;
z = 14;
myTransform = transform;
myTransform.position = new Vector3(x,y,z);
currentSpeed = Random.Range(minSpeed,maxSpeed);
}
// Update is called once per frame
void Update ()
{
x= Random.Range(-12,12);
myTransform.Translate(Vector3.back * currentSpeed * Time.deltaTime);
//everytime the enemy object goes beyond z=-6 it dissapears and creates a new enemy object
if(myTransform.position.z < -6)
{
myTransform.position = new Vector3(x,y,z);
currentSpeed = Random.Range(minSpeed,maxSpeed);
}
}
void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.CompareTag("Projectile"))
{
Destroy(this.gameObject);
}
}
}
Out of context of your app, it is hard to say what is the right approach. You either have Instantiate() a new game object, or you have to reuse this one. If you elect to reuse, in some games you can just teleport it and reset any state you need. In other games, you need to have an object pool. Search UA for a number of answers for both simple and more complex pooling solutions.
While folks can give you opinions, as this questions stands now, there is not enough information for a specific technical solution. And it is a design question that likely belongs on Unity Forums rather than here.
do you want to create 1 more everytime one is destroyed? or do you want to create 1 every second or something?
Answer by nightbane30 · May 07, 2014 at 01:11 AM
Now, there wasn't too much info that you provided us with for us to give a solid answer, but I'd say use the Instantiate method. Here's an example of it in use:
void OnTriggerEnter (Collider collider) {
if(collider.gameObject.CompareTag ("Projectile")){
//Right now, all you have is this
Destroy(this.gameObject);
//If you add this, it should instantiate a new enemy prefab
Instantiate(EnemyPrefab, EnemyPrefabPosition, Quaternion.identity);
//the quaternion is just to show that there's now rotation
//I used placeholder names as well.
//Check the scripting reference for Instantiate for more info.
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating scene from code 1 Answer
Rotation Speed 1 Answer
Respawn timer? Duplicate instance OnDestroy question... 0 Answers