Thursday, March 9, 2017

c# - Unity, check if all GameObject tagged are destroyed


Hello im a total noob so please dont blame me. So, I have this code but I cant find a fitting answer on how to check if everything tagged "Target1" is destroyed. Most of the answers are either overcomplicated or ,and most certainly, im too stupid


public Transform teleportTarget;
public GameObject thePlayer;
public Transform[] spawnLocations;

public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;

void spawnTargets()
{
whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
whatToSpawnClone[1] = Instantiate(whatToSpawnPrefab[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
whatToSpawnClone[2] = Instantiate(whatToSpawnPrefab[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
whatToSpawnClone[3] = Instantiate(whatToSpawnPrefab[3], spawnLocations[3].transform.position, Quaternion.Euler(0, 180, 0)) as GameObject;
}


private void TeleportPlayer()
{
thePlayer.transform.position = teleportTarget.transform.position;
}

Im just looking for a way to check if all GameObjects Tagged "Target1" (in the whole game) are destroyed and then to spawnTargets and TeleportPlayer. Previously I had the bug, that when they respawned it wasnt checking anymore. It has to check all the time.Because else I get stuck, because teleport teleports me all the time. I hope you understand what I mean


I know this is a easy question but im a noob and im sorry for wasting your time, still I hope you can help me.



Answer



I want to check if all GameObjects tagged "Target1" have been destroyed, call a function like what you want for example instantiate something and etc...



here is code that you want:


void Update(){
if(GameObject.FindGameObjectsWithTag("Target1").Length <= 0)
//call your function to spawn targets and teleports
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...