- Home /
Respawn Enemy Prefab after its destroyed
Ive added a script onto my enimes that makes it once they are destroyed they will respawn the rigidbody placed in "respawn" and at the spot "respawnpoint" my problem is that once respawned they are called "Clones" and after two respawns the enemy no longer has the scripts working.. how do I edit this script to spawn my enemy not as a clone?
using UnityEngine;
using System.Collections;
public class AttackandHP : MonoBehaviour {
public int maxHealth = 10;
public int curHealth = 10;
public GameObject target;
public float attackTimer;
public float coolDown;
public float EXPGAIN = 20;
public GUIText DamagePoints;
public int damage = 0;
public Transform ptsPrefab;
public Rigidbody respawn;
public Transform respawnpoint;
public float healthBarLength;
// Use this for initialization
void Start () {
target = GameObject.FindGameObjectWithTag ("Player");
healthBarLength = Screen.width / 2;
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
void OnGUI() {
}
public void AdjustCurrentHealth(int odj) {
curHealth += odj;
if(curHealth < 1)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
if (curHealth == 0) {
Destroy (gameObject);
Rigidbody clone;
clone = Instantiate (respawn, respawnpoint.transform.position, respawnpoint.transform.rotation) as Rigidbody;
PlayerAttack ZS = (PlayerAttack)target.GetComponent ("PlayerAttack");
ZS.DeselectTarget ();
ZS.SpawnEXP();
PlayerHealth eh = (PlayerHealth)target.GetComponent ("PlayerHealth");
eh.AdjustCurrentEXP (20);
}
Answer by nesis · Jan 15, 2014 at 01:52 PM
You're destroying the enemy gameObject and are replacing it with something that doesn't necessarily have all components your original gameObject had, because it's being created from a Rigidbody.
Save the enemy as a Prefab, change respawn's type to be GameObject, and then after selecting the enemy in the Inspector, set respawn's value to whatever the enemy's prefab is in the Hierarchy tab. That way you'll be spawning fully-fledged copies of your enemy.
That said, you seem like you don't want to destroy the enemy so much as reset its health and other stats, and place it back at its spawn point. So instead of using Destroy(gameObject), you could just write a method to reset the enemy's stats, position, and orientation to what they were when the enemy first spawned.
For a quick and dirty example:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
private Vector3 startPosition;
private Quaternion startRotation;
private float startHealth;
public float health = 50f;
public void Start() {
//record starting values of relevant variables
startHealth = health;
startPosition = transform.position;
startRotation = transform.rotation;
}
public void TryDamage(float amount) {
health -= amount;
if (health<0f) {
KillAndReset();
}
}
//call this when the enemy needs to be killed and reset
public void KillAndReset() {
health = startHealth;
transform.position = startPosition;
transform.rotation = startRotation;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How kill an enemy and respawn after die 2 Answers
Enemy not generating... 0 Answers
Enemy respawning after death 1 Answer
enemy respawn after amount of time 2 Answers
I Can't make 3D Enemy Random Spawn and this Random Moving.. 0 Answers