how to Spawning a single enemy, respawn when object not active?
Hello, I am a beginner at unity. I am trying to make an enemy spawn in my 2d game. I only want a Single Enemy to spawn, once the enemy is deactivated, a new one should spawn in its place. When the enemy reaches the collector object it should become inactive - this part works.
I can't get the spawner to spawn more enemies. I was hoping someone could tell me where I am going wrong. I think that it is because i ony initialize in awake - but i cant initalize in a fixedupdate() Thank you very much in advance any help is appreciated!
Collector Class - works fine when using active objects in the scene
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Walking Zombie")
{
target.gameObject.SetActive(false);
}
}
Spawner class - currently not spawning
public GameObject walkingZombie = GameObject.FindGameObjectWithTag("Walking Zombie");
void Awake()
{
InitializeWalkingZombie();
}
void Start()
{
}
void InitializeWalkingZombie()
{
if (walkingZombie.activeInHierarchy == false)
{
Instantiate(walkingZombie
, new Vector3(Random.Range(8.19f,8.12f)
, Random.Range(-6.73f,1.75f), 0), Quaternion.identity);
}
}
Answer by Taxen0 · Feb 04, 2016 at 03:26 PM
I don't see you actually calling InitializeWalkingZombie() anywhere, besides once in the start? so no more Zombies would spawn.
Is there a particular reason for not using Destroy() instead of SetActive(false)? As you are trying instantiate a new zombie object it you would end up with a lot of unused ones eventually.
Maybe a change like this would help you?
collector
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Walking Zombie")
{
Destroy(target.gameObject);
//call a method that spawns a zombie. - you need a reference to the spawner class.
spawnerClass.InitializeWalkingZombie();
}
}
spawner
void InitializeWalkingZombie()
{
Instantiate(walkingZombie
, new Vector3(Random.Range(8.19f,8.12f)
, Random.Range(-6.73f,1.75f), 0), Quaternion.identity);
}
@Taxen0 Originally I tried putting the InitializeWalkingZombie inside a FixedUpdate but Unity yelled at me for it telling me i can only instantiate in a start or awake.
Perhaps I misread this- but i thought that deactivating an object was better than destroying. I only have two zombie prefabs, I figured the code would kind of juggle through them so one is active, then the other is inactive etc. Is this not a good Idea?
I tried implementing your idea, however im kind of stuck on calling the method from the spawner class in my collector class. Do I need to make an instance of the spawnerClass in the Awake() function? If you could give me some more detail on how to do that id be really greatful!
Thank you for your response by the way.
FixedUpdate() should be used for anything physics related, however the regular Update() method should have worked fine. I would not recommend having it there either though as it would have to perform the check every frame, ins$$anonymous$$d of only when it is needed (the death of a zombie?).
using SetActive to enable and disable object works as well, and in some cases it is better than using Destroy. Imagine that you have a gun that shoots many bullets, ins$$anonymous$$d of instantiating every bullet when it's fired and destroying them once they hit something we can just re-use the same bullets by setting their active state. this is commonly known as "pooling" and saves quite a bit of performance.
But if you only have 1 zombie active at one time that is nothing you need to think about as there wouldn't really be any gain.
Co$$anonymous$$g to think of it, if you only ever is going to have one Zombie you could also just relocate that one whenever you "kill" it. that could be done like this:
collector
void OnTriggerEnter2D(Collider2D target)
{
ZombieScript zombie = target.GetComponent<ZombieScript>();
zombie.Respawn();
}
ZombieScript (you can call this whatever you want, as long as it is attached to the zombie object)
public void Respawn(){
transform.position = new Vector3(Random.Range(8.19f,8.12f), Random.Range(-6.73f,1.75f), 0);
//add other stuff you might need to reset upon respawn
}
Sorry I should have clarified this earlier. The player is just avoiding the zombie not killing it which is why i have a collector object out of the camera's view so when the zombie hits the collector it should deactivate, then when the spawner realizes there is not active zombie it should spawn a new one.
@Taxen0 just to clarify, if i use the idea mentioned above - Can I use this respawning method inside my spawning class ins$$anonymous$$d of my zombie class? - the camera moves foward so i'm not sure how id grab the coordinates, Im currently away from my comp but here is a drawing of what im trying to do
sure why not!
you could make something like this in the spawner class:
private GameObject walkingZombie;
void Awake() {
walkingZombie = GameObject.FindGameObjectWithTag("Walking Zombie");
}
Start() {
RepositionZombie();
}
void RepositionZombie() {
walkingZombie.transform.position = new Vector3(Random.Range(8.19f,8.12f), Random.Range(-6.73f,1.75f), 0);
}
The collector looks almost like before:
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Walking Zombie")
{
spawnerClass.RepositionZombie();
}
}
you just need to get a reference to the spawnerClass inside you collector and it should work, and have one zombie anywhere in the scene at launch.
Thank you so much for all your help i really appreciate it. I was wondering if you need to reference the spawner class in a certain way? for some reason when i use a static instance, or if i use get component it does not find the method i want. - unity tells me that the object reference not set to an instance of an object...but i thought thats what my code is doing? Online I am seeing a variety of ways to do call the method, but what is really the best practice way of doing it? I dont think that my Initialize method should be public or static Spawner class
public static WalkingZombieSpawner instance;
void InitializeWalkingZombie(){
Instantiate(walkingZombie, new Vector3(Random.Range(8.19f, 8.12f), Random.Range(-6.73f, 1.75f), 0), Quaternion.identity);
}
Collector class
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Walking Zombie")
{
target.gameObject.SetActive(false);
WalkingZombieSpawner.instance.InitializeWalkingZombie();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612063001im_/https://answers.unity.com/themes/thub/images/avi.jpg)