- Home /
Enemy Respawn After Death (MMO Style) C#
So I have looked around a lot and not really found anything that was able to help em figure out a way of spawning enemies after they were successfully destroyed with a waiting time.
I was thinking about something like World of Warcraft. Where if you kill the enemy it will take about 1-3 minutes till he would spawn back!
.
I have no script attempting and no other topic that helps me! So if you can help me figure this out or give me a Topic that has my question/problem in it, and shared it with me! that would be very kind!
Thank you in advance!
Answer by LoneWolfSwat · Apr 26, 2017 at 11:02 AM
Create a new C# Sharp script called "Respawn" Create a new Empty Object in the scene, called "Respawn point" Attach "Respawn" to the Respawn point Open "Respawn"
Add all this:
public class Enemyrespawn : MonoBehaviour {
public bool Death;
public float Timer;
public float Cooldown;
public GameObject Enemy;
public string EnemyName;
GameObject LastEnemy;
// Use this for initialization
void Start () {
//If you want, add this line:
Death = false;
this.gameObject.name = EnemyName + "spawn point";
}
// Update is called once per frame
void Update () {
if(Death == true) {
//If my enemy is death, a timer will start.
Timer += Time.deltaTime;
}
//If the timer is bigger than cooldown.
if(Timer >= Cooldown) {
//It will create a new Enemy of the same class, at this position.
Enemy.transform.position = transform.position;
Instantiate(Enemy);
LastEnemy = Find(Enemy.name + "(Clone)");
LastEnemy.name = EnemyName;
//My enemy won't be dead anymore.
Death = false;
//Timer will restart.
Timer = 0;
}
}
}
Go to your enemy health script and add:
if(Health <= 0) {
//Do everything you want with this part, but before destroying the enemy, add this:
GameObject.Find(gameObject.name + ("spawn point")).GetComponent<Enemyrespawn>().Death = true;
//Then destroy it
Destroy(gameObject);
}
----------------------------------------------------- Move your empty object to a position. (In that position your enemy will respawn)
Now, the final part. Make a prefab, and throw your enemy in there. Go to your spawner: Recommended: Death = false; Timer = 0; Cooldown = (if 3 minutes, 180 seconds) 180 Enemy = THE PREFAB, NO THE OBJECT IN THE SCENE Enemy name = Your enemy's name...
The name `Find' does not exist in the current context
Even GameObject.Find doesn't work lol---
LastEnemy = Find(Enemy.name + "(Clone)");
I'm so sorry:
LastEnemy = GameObject.Find(Enemy.name + "(Clone)");
It's the only solution... What error does it say when you try GameObject? Have a nice day.
Answer by sadowlight123 · Apr 25, 2017 at 09:33 PM
you need to follow a tutorial on any spawning items, it will help you understand it better , try going on youtube and type spwan items in unity you will get plenty of results. here are some tips you might find interesting. 1- you will need a timer 2- you will need to set spawning points and pick them randomly or set a boxlike boundaries where your enemies can spawn into 3- you will need to use the Instantiate function to create a new enemy (this function will take the gameobject prefab ,plus its position and its rotation) 4-you will need to trigger the timer to start it's countdown after the object has been destroyed and to reset and freeze that counter when the new enemy has been spawn
hope it helps have a good day :)
Answer by Slatey · Apr 26, 2017 at 04:41 AM
steps i would use: 1. create a spawner object. it has information like what mobs can spawn, maximum number of spawns, respawn time, area mobs can spawn in. 2. spawner object runs a timer to spawn the mobs. you can have the start event spawn the maximum number if you want or a subset of that. 3. spawned mobs can be kept in a list on the spawner object. this can help in getting the count and you might employ other uses. 4. once the spawner has spawned the maximum mob count, it can switch the timer off. 5. if a mob is killed, it tells the spawner it has been killed so the spawner can start the timer again.
you could also just leave the timer on, then just check if the maximum mob count has spawned so another won't be spawned on that timer tick.
Answer by Halwasa · Mar 21, 2018 at 12:10 AM
isent there a better way than useing update ?? because calling update to check is not a good code optimize wise. I tried using OnDestroy but I am having a trouble:
.
1) the enemy being back as the last state it was and inactive . its an Empty gameObject and I used Instantiate(Enemy,transform); // because the enemy is a child of the object so it should come out from the Transform of the parent // it did work sometimes but after cleaning the code it doesn't work and when I uncleaned it it stayed like that..
2) when I close the playtest I get a nullreferenceexception this is properly because the OnDestroy Get to run when the game end. I dont know how to change that so it does not be called when the game ends because I think it would cause a game break when it get built.Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enemy prefab wont work with the enemy movement script 3 Answers
Why does this prefab trigger all instances to action? C# 1 Answer