- Home /
Enemy Death and Respawn??
Hi,
I am a beginner at C# and Unity and working on a mini game where the player (a capsule) fights off a horde of enemies (Other Capsules). The player can shoot a bullet that can kill the enemy.
I want to add death and respawn elements onto the enemies. For example, If I shoot and kill one enemy, they die and can respawn.
How would I go about making this in C# and in the Unity Editor??
What have you done so far? We don't make the game, you do :)
Answer by Cole_Slater · Oct 11, 2017 at 12:14 AM
@KillScope-9000 What I might do for something like this is create some script on an empty game object that acts as a GameManager, from where you can spawn the aliens. As for killing the aliens, you could just create a public KillAlien() function within the Alien script that can be called from either within the script or from somewhere else if need be. Not sure if this works, but just trying to give you an idea for something you can do.
//spawn aliens. This would go on a script called GameManager on empty GameObject or something
public GameObject alien;
public Vector3 spawnPoint;
void Start(){
SpawnAlien(); //example of calling a function from within the script
}
public void SpawnAlien(){
//spawns alien gameObject at spawnPoint position, and give it a rotation to complete the function
Instantiate(alien, spawnPoint, Quaterinion.Identity);
}
KillAliens(). This would go somewhere in the Alien script, and could be called when say its health is at 0. you are going to need something to call the KillAlien() function for this whole thing to work
KillAlien(){
//gets reference to script called "GameManager", and calls the SpawnAlien() function to respawn itself
GameManager gameManagerReference GameObject.FindObjectWithType<GameManager>();
gameManagerReference.SpawnAlien();
//destroys itself after calling SpawnAlien() function
Destroy(gameObject);
}
Answer by Abzi94 · Oct 11, 2017 at 04:19 AM
First you want to instantiate enemy, then is they dies destroy them, and run a coroutine which will wait for some time for enemy re spawn and for re spawning instantiate them again simple enough.
For $$anonymous$$illing use Destroy function and destroy function can handle time. eg Destroy(gameObjectName,3);//3 is time after which object will be destroyed . For Spawn make a GameObject Array and instantiate them if enemies are different . public GameObject enemy; Instantiate(enemy,spawnPoints[spawnPointIndex].position,spawnPoints[spawnPointIndex].rotation);
Answer by Anonymous700 · Oct 11, 2017 at 04:50 AM
For Killing use Destroy function and destroy function can handle time. eg Destroy(gameObjectName,3);//3 is time after which object will be destroyed
. For Spawn make a GameObject Array and instantiate them if enemies are different .
public GameObject enemy; Instantiate(enemy,spawn[spawnPointIndex].position,spawnPoints[spawnPointIndex].rotation);
// first parameter is enemy 2nd is position where to spawn and 3rd is rotation .