Making enemy respawn after death

Hello Unity3D community I am currently creating a game for a Game Programming and Design class and I am running into a few issues. I am trying to respawn the enemy after they are kill (SetActive(false)) but for some reason when the enemy’s position is reset to their initial position and SetActive is returned to true the enemy no longer behaves as the script dictates. I have also tried instantiating an enemy game object with prefab and destroying the original enemy but the prefab’s script won’t allow me to attach the necessary things to it. I would appreciate it if anyone could give me some help or pointers.
Here is the link to my github repo—> https://github.com/wickedRidge/RagnarokUnity3D

To simplify things here is a copy of my enemy’s (wannabe) AI script

#pragma strict

var myTransform : Transform;
var ragnarok : PlayerController;
var drood : EnemyBehavior;
var target : Transform;
var playerPos : Vector3;
var eBullet : Rigidbody;
var enemyRespawnPoint : Transform;
var eSpawnPoint : Transform;
var scoreText : GUIText;
var winText : GUIText;
var lSound : AudioClip;
var boom : AudioClip;
private var eHP : int;
private var score : int;
var speed : float;
var delay : int;
private var killed : int;
var shoot : boolean;
var move : boolean;

function Start () {
myTransform = transform;
eHP = 8;
killed = 0;
score = 0;
scoreText.text = “”;
enemyStat();
winText.text = “”;
yield WaitForSeconds(1.5);
shoot = true;
move = true;
}

function Update () {

playerPos = target.position;
myTransform.position += myTransform.forward * speed * Time.deltaTime;
var lookDirection : Vector3 = target.position - myTransform.position;
var lookRot : Quaternion = Quaternion.LookRotation(lookDirection);
if(move){
myTransform.rotation = lookRot;

move = false;
lockOn();
}
if(shoot){
var eLaser : Rigidbody = Instantiate(eBullet, eSpawnPoint.position, lookRot);
eLaser.rigidbody.AddForce(playerPos * 100);
audio.PlayOneShot(lSound);
shoot = false;
shootAgain();
}
}
function shootAgain(){
yield WaitForSeconds(delay);
shoot = true;
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == “Bullet”){
other.gameObject.SetActive(false);
eHP = eHP - 1;
score = score + 5;
enemyStat();
}
}
function enemyStat(){
scoreText.text = "Score: "+ score;
if(eHP == 0){
gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(boom, myTransform.position);
killed++;
respawn();
}if(killed == 2){
winText.text = “Victory!!!\n”
+“Press UP to go to the title screen”;
ragnarok.done = true;
}
}
function lockOn(){
yield WaitForSeconds(.1);
move = true;
}
function respawn(){
gameObject.transform.position = enemyRespawnPoint.position;
gameObject.SetActive(true);

}

Well then:

  1. Please use code tags =)
  2. What does “wrong berhaviour” in this case mean? Doing nothing?
  3. One initial thought: Do you do what you are currently doing in the start function as well when you reset / respawn the player?
    Cheers

As flashmuller wrote, because you have items being set on start, their not being reset on respawn(as your not actually respawning them).

Try putting those values into a new function, calling that function both on start and after resetting your object to its respawn. It might not fix your errors, but will help track them down, and save future ones.

Also code brackets; )

wouldn’t it be easier to just do this
reset its target to null
stop its attack
play death animation for enemy
move it to its re-spawn position and reset all it’s variables to initial values
wait however long you want the re-spawn time to take
then give it a command to patrol or look for player again …all without changing it’s active state

the reason i say this is because when you first set active to false, that happens at the end of the current update…(not right when called)
also …if this script is attached to your enemy object seting as active won’t occur as the script isn’t being executed since the object is inactive

just my thoughts