- Home /
checking to see if multiple (different) tags are destroyed
hello neighbors! another little conundrum for you brilliant and tech savvy genius' at unity answers! i have a little destroy check script here.
function Update() { var enemies = GameObject.FindWithTag("EditorOnly"); //find gameobjects with the tag "EditorOnly" if(enemies == null){ Application.LoadLevel("deathscene1"); } }
ok, great. this kinda works. problem is... when i send one object into my destroy trigger with (tag)... all of them are destroyed because they all have the same tag. so now i need to re-tag all of the objects i want to be destroyed individually. and i need this script to check for each of the tags... and not load my deathscene level until all of them return (null) any help would be great... i will be posting a second part to this puzzle that focus' on the actual destroy object scripts. but i really need to get this working first. thanks again and i hope to hear from you!
the tags i am using are : fluf1, fluf2, fluf3, fluf4, fluf5, fluf6
i tried this... and yielded no result. i think it is because my "if" statement doesn't properley identify the tags in question. but i cant figure out how to write it out. basically, all this script is supposed to do, is check to see if the objects with said tags are still present in the scene... if they have all been destroyed and no longer exist than i would like to load a new scene. im sure you gathered that just by looking at the script though. can't anyone shed some light on this? i have been giving myself a headache thinking about it. any help would be great.
import System.Linq;
var tags = ["fluf1", "fluf2", "fluf3", "fluf4", "fluf5", "fluf6",];
function Update; if (tags == null; Application.LoadLevel("deathscene1"); } }
Answer by inkspot · Jul 18, 2013 at 01:16 AM
If i understand it right, the deadscene1 is loaded, when there are no enemies left in the current scene.
"when i send one object into my destroy trigger with (tag)"
Do you refer with this 'destroy trigger' the script snippet you use above ?
for your second snippet i show you an example of the principle
var tags = ["fluf1", "fluf2", "fluf3", "fluf4", "fluf5", "fluf6",];
function Update() {
if (tags.length == 0) { /// checks if the array empty
Application.LoadLevel("deathscene1");
} }
But i think your making things too complicated for yourself this way.
inkspot, this script is used to just check the scene for tags. when all of the tags are destroyed... i want the script to load the level. i tried setting up the array... i can't seem to get it. it just does nothing. i dont get it at all. your script seems like it should work fine... thanks for trying.
Hi mattlewis83,
Sorry to hear you didn't manage to get your script working, my example was just a rough way to show how idea works. Here is a more workable code it works a bit different i added your 'destroy trigger' to work with it (haven't tested that part)
#pragma strict
//var enemylist : Array = new Array();
var enemycount : int;
function Awake () {
/// find all objects in the scene with the tag [your tagname here]
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("EditorOnly"); /// or what tag you prefer
for (var go : GameObject in gos) {
enemycount++;
}
Debug.Log("enemies found "+enemycount); // shows you how much enemies were found
}
/// call this function when an 'object' is destroyed // haven't tested it
function destroytrigger () {
// check if there are enemies left
if(enemycount !=0) {
// remove one from enemycount
enemycount--;
} else {
// no more enemies left, load new scene
Debug.Log("load new scene");
}
}
inkspot... how would i attatch this? i am curious if this should go on the object that actually destroys the object upon entering the trigger... on a master object in the background... or on each enemy being destroyed. im assu$$anonymous$$g not on the enemy being destroyed because the script will destroy this script. how would you set something like this up. i have work till nine and will check everything out then. you have been very helpful!
This script is tailored for your question, and the issue your having, best way however is to make a script dynamic, so you can use it in every scenario/level, for example right now the script is based on 'already having enemies in the scene' on start, but maybe you want to spawn more enemies on the run. or spawn them not right after start etc.
Which is easy because you can directly add those new spawned enemies to that enemycount integer, all within one script and you have a A to Z script that starts to work as a 'manager'. that you deploy in every scene/level for handling enemies.
you could for example build on top of this script like adding a score system (add it into the destroytrigger function), or add a new function for spawning enemies (and add their numbers directly to that enemycount integer)
About using this script, it's easy, you add an 'empty gameobject' to act merely as a holder for this script, as you already described "a master object in the background" and let other objects/enemies communicate with it. to tell them their dead and need to be removed from the counter.
Answer by robertbu · Jul 18, 2013 at 02:29 AM
I think you are approaching the problem backwards. You should tag all your items the same...give them all a 'fluf' tag for example. If you need to distinguish between the various fluf objects, then either give them unique names (not unique tags) like ('fluf1', 'fluf2'...), or attach a script to your fluf game objects with a string for a name. You can use GetComponent() to get the script and then the name. If there is a really compelling reason to use this many different tags, you will need to make multiple FindWithTag() calls, one for each tag. You can use an array of tag names and then use a for loop to cycle through the list.
ok, so the script that inkspot provided didn't work. i originally had all the enemies tagged with the same tag and used : function Update() { var enemies = GameObject.FindWithTag("EditorOnly"); //find gameobjects with the tag "EditorOnly" (you must therefore tag the enemies with "enemy" if(enemies == null){ //if no gameobjects with the tag "enemy" are found
//change scene
Application.LoadLevel("deathscene1"); } } ... but if one fluf entered the trigger... all would dissapear. so i decided to tag them differently and use a tag check script like the one inkspot provided above with no result. i wonder why this isn't working. i can provide an example of what is happening... but i fear it is going to be too confusing for most to grasp. it's kinda retarded. it's my fault.
robertu do you have an example? i tried to rewrite the script so it basically works the way you suggested but still find myself stuck. var tags = ["fluf1", "fluf2", "fluf3", "fluf4", "fluf5", "fluf6",]; function Update() { for (var tagsVar in vartags) { ...loop body... } FindWithTag(fluf1); FindWithTag(fluf2); FindWithTag(fluf3); FindWithTag(fluf4); FindWithTag(fluf5); FindWithTag(fluf6); if (tags.length == 0) { /// checks if the array empty Application.LoadLevel("deathscene1"); } }
I still think you should use a single tag. Use gameObject.name in your trigger code to get a unique value. But if you must check each tag, here are two functions.
Using an array:
var tags = ["fluf1", "fluf2", "fluf3", "fluf4", "fluf5", "fluf6"];
function AllDead() : boolean {
for (var tag in tags) {
if (GameObject.FindWithTag(tag) != null) {
return false;
}
}
return true;
}
Or just brute force:
function AllDead2() : boolean {
if ( (GameObject.FindWithTag("fluf1") == null)
&& (GameObject.FindWithTag("fluf2") == null)
&& (GameObject.FindWithTag("fluf3") == null)
&& (GameObject.FindWithTag("fluf4") == null)
&& (GameObject.FindWithTag("fluf5") == null)
&& (GameObject.FindWithTag("fluf6") == null)
) return true;
return false;
}
thanks robertbu... i will be sure to try this method also. i spent all night trying to get an array written and i was writing snippets of code in my sleep. it sucked. i have really been racking my brain over this because this is a key function in getting the game to be a game. now say if i want to load a level after using the array.... would i add the application.loadlevel after the return true? and what should i attach this to? thanks for your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613110310im_/https://answers.unity.com/themes/thub/images/avi.jpg)