- Home /
Respawn prefab after you destroy it
is it possible to respawn a prefab once it is destroyed.
ie.
Player hits trigger spawns enemy. then you kill the enemy and it destroys prefab. then if you hit the trigger again it respawns the same enemy to kill again. I just cant figure this out.
Answer by Lovrenc · Jan 13, 2013 at 11:51 PM
You should NEVER DESTROY THE PREFAB.
Destroy instance of a prefab.
how do you do that destroy the instance of the prefab?
ie.
Instantiate(spawnobject, spawnspot[spawnedEnemies].transform.position, Quaternion.identity);
but how do you destroy the Instantiated object?
function Update(){
SetPlayerName = Enemy.name;
Text$$anonymous$$eshName.gameObject.GetComponent(Text$$anonymous$$esh).text = SetPlayerName + "\n\r" + hp;
if (Input.Get$$anonymous$$eyDown("1")) {
if (wasClicked) {
wasClicked=true;
audio.PlayOneShot(SmiteSound);
//instantiate the projectile
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
//move the projectile
instantiatedProjectile.velocity = (enemies_list[ai$$anonymous$$g_at].GetComponent(Transform).position - transform.position)*speed;
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
// Send a damage message to the hit object
DamageNumber = Random.Range(1,(HitPoints/6));
print ("hit : " + ai$$anonymous$$g_at + " with " + DamageNumber + " damage");
hp = hp -= DamageNumber;
Destroy(instantiatedProjectile.gameObject, ScorePoints);
//print ("ai$$anonymous$$g at : "+ai$$anonymous$$g_at);
}
}
if(ai$$anonymous$$g == true)
{
look_at = Quaternion.LookRotation(enemies_list[ai$$anonymous$$g_at].GetComponent(Transform).position - transform.position);
look_at.x = 0;
look_at.z = 0;
transform.rotation = Quaternion.Lerp(transform.rotation, look_at, track_speed);
if (Input.Get$$anonymous$$eyDown("-"))
{
if (ai$$anonymous$$g_at < enemies_list.length)
ai$$anonymous$$g_at++;
else
ai$$anonymous$$g_at = 0;
}
if (Input.Get$$anonymous$$eyDown("="))
{
if (ai$$anonymous$$g_at > 0)
ai$$anonymous$$g_at--;
else
ai$$anonymous$$g_at = enemies_list.length -1;
}
}
if (hp <= 0)
Destroy(gameObject);
if (hp <= 0)
{
print ("scores: " + PlayerPrefs.GetString("playerName") + "--" + ScorePoints);
//Instantiate(Explode, transform.position, transform.rotation);
postScore(PlayerPrefs.GetString("playerName"),ScorePoints);
}
}
that is the update function with the destroy in it.
teh specific line is :
if (hp <= 0) Destroy(gameObject);
i would gusee it is there that i destroy the instantiated on not the actual game object itself? correct?
sorry it put it in three boxes.. dont know why it did that but it is all in one block...
Because 4 spaces mean it should format text as code on this forum.
You're good, you arent destroying prefabs. Where is this script attached to?
Your answer
Follow this Question
Related Questions
Did triggers inside loaded gameobjects break in a recent update? 0 Answers
Is there any way to see exactly which triggers are overlapping? 1 Answer
How to enable passing through collider if speed is high enough at collision point 0 Answers
On Trigger Enter just triggers once for same object 1 Answer
Problem: One Trigger activating a seperate Trigger it is not in contact with. 1 Answer