If all tagged objects are destroyed
This script should respawn Objects with the Tag Target1 but instead of respawning when their all destroyed they just duplicate immediently an when I destroy all they don't respawn. pease help thx :D
void Start()
{
if (respawns == null)
respawns = GameObject.FindGameObjectsWithTag("Target1");
foreach (GameObject respawn in respawns)
{
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation);
}
}
i dont understand... are you instantiating objects at same place where you find an object?
i mean, you do foreach respawn, instantiate prefab in respawn.position
and of course, if a respawn object is destroyed, the foreach will not iterate it... so if all destroyed, the foreach cant do nothig..
Respawn is a emptyobject? or what?
im sorry I have a variable called respawn prefab and a variable for GameObject
Ah! i think i read now correctly your post. (Please use comas...) You want to wait until all respawn objects are destroyed, and then respawn them all at once.
Is this correct?
Answer by tormentoarmagedoom · May 24, 2019 at 07:45 AM
Hello.
Then you need first, detect when are all destroyed, and then, need to instantiate the prefabs in the positions. The "problem" is that the positions can not be obtained form the objects, because they are destroyed.
So First, you need to do something to know the positions without need the objects. (you can sotre its Vector3 position on destroy, or have emptyobjects at the respawn points...)
To detect if they are all destroyed, you need to remake the array of "respawn" objects every time, and when its lenght is 0 means all are destroyed
respawns = GameObject.FindGameObjectsWithTag("Target1");
if (respawns.Length = 0)
{
Spawn them all
}
And as I said, you need something to know the positions where the objects was before beeing destroyed. I'm sure you will find a way!!
Bye!
Your answer
![](https://koobas.hobune.stream/wayback/20220612201944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My respawning script isn't working 0 Answers
Accessing objects in C# 1 Answer
Creating and destroying SELECTED objects and... sorting arrays?? 0 Answers
Hi! I am trying to use SceneManagement, but I keep getting errors. 1 Answer
My Brain is fogged: How do I move a networked object with user input 0 Answers