- Home /
How to Respawn Ai Enemies After Destroy Gameobject??
I have a bullet script that sends out a message to deduct health if what the Raycast hit was tagged with enemy. I also have a health script that when it recives the message, it takes away the var damage (25). In this script, when it reaches "0", the game object is destroyed. All works but when I add a respawn part.... well it kind of works.... I mean the Ai respawns and all but it no longer chases me. I also have the Ai as a prefab thinking that this would help.
(HealthScript) SCRIPT: #pragma strict
var Enemy : GameObject;
var Health = 10;
function ApplyDammage (Dammage : int)
{
Health -= Dammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
Health = 10;
Respawn();
}
function Respawn()
{
Instantiate (Enemy, Vector3(7,2.18094,0), Quaternion.identity);
}
Nothing in this script is helpful in figuring out what is going on. Likely the issue is in your Enemy script.
On your Prefab you have the AI script attached to the Enemy Prefab? I am assu$$anonymous$$g you do. When it respawns do you see the script on the newly instantiated enemy?
Ha I found the Problem! its not solved but I still found out whats going on. When I play the game, my Ai moves perfectly but once its dead it doesnt move. Seeing this, I investigated. I later found out that the script for health and Ai were unchecked, not when I start the game but when the Ai has died and respawned. Anyone know how to fix this???
Answer by pdunton · Jul 21, 2013 at 02:47 AM
Im not very good at coding, but this sounds like some problem i had. Try instead of destroying the object, scaling it really really really small, and coding it to stop the AI. And then upon respawn, scale it back up and restart the AI. Sorry if this is no help.
Answer by Dougbott · Jul 23, 2013 at 02:06 PM
To enable/disable scripts or any other "stuff" on your gameObject, just use this:
//This is to check it
gameObject.GetComponent<myAIScript>().enabled = true;
//This is to uncheck it
gameObject.GetComponent<myAIScript>().enabled = false;
Hope this helps! Good luck!
Answer by coAdjoint · Jul 23, 2013 at 02:16 PM
You need a game manager script, like that used in the AngryBots project that comes with Unity.
The GameManager class works by instantiating a number of enemies at the start of the game. When these enemies are 'killed' in the game, they are deactivated and reset.
You may also want to check out this video by Prime31
http://www.youtube.com/watch?v=IX041ZvgQKE
You'll also learn about LINQ at the same time!
Answer by Halleflux · Jul 24, 2013 at 09:22 AM
gameObject.GetComponent(myAIScript).enabled = true;
Same as Dougbott's answer, only in JavaScript.
A good idea, if you are using the script more than once, it to put it in a variable:
var scriptName : ScriptName;
scriptName = gameObject.GetComponent(ScriptName); //As a side note: you can get other GameObject's components with this script. This is useful in many circumstances.
Your answer
Follow this Question
Related Questions
2D top-down shooter AI 0 Answers
Different enemy types script design 1 Answer
AI Target Finding Optimization 0 Answers
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer
How to add respawn to enemy AI? 1 Answer