• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by mattlewis83 · Jul 17, 2013 at 07:09 PM · triggerdestroymultipletagsenter

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.

  1. function Update() { var enemies = GameObject.FindWithTag("EditorOnly"); //find gameobjects with the tag "EditorOnly" if(enemies == null){ Application.LoadLevel("deathscene1"); } }

ok, great. t$$anonymous$$s 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 t$$anonymous$$s 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 t$$anonymous$$s puzzle that focus' on the actual destroy object scripts. but i really need to get t$$anonymous$$s working first. thanks again and i hope to hear from you!

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattlewis83 · Jul 17, 2013 at 05:16 PM 0
Share

the tags i am using are : fluf1, fluf2, fluf3, fluf4, fluf5, fluf6

avatar image mattlewis83 · Jul 18, 2013 at 12:56 AM 0
Share

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"); } }

avatar image mattlewis83 · Jul 19, 2013 at 04:22 PM 0
Share

alt text

flufinstructions3.gif (288.7 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image

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 t$$anonymous$$s '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 t$$anonymous$$nk your making t$$anonymous$$ngs too complicated for yourself t$$anonymous$$s way.

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattlewis83 · Jul 19, 2013 at 03:47 AM 0
Share

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.

avatar image inkspot · Jul 19, 2013 at 11:22 AM 0
Share

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");
   }
 }

 
avatar image mattlewis83 · Jul 19, 2013 at 03:13 PM 0
Share

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 assuming 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!

avatar image inkspot · Jul 19, 2013 at 04:41 PM 0
Share

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.

avatar image

Answer by robertbu · Jul 18, 2013 at 02:29 AM

I t$$anonymous$$nk you are approac$$anonymous$$ng 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 t$$anonymous$$s 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.

Comment

People who like this

0 Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattlewis83 · Jul 19, 2013 at 03:24 AM 0
Share

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.

avatar image mattlewis83 · Jul 19, 2013 at 03:38 AM 0
Share

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"); } }

avatar image mattlewis83 · Jul 19, 2013 at 03:45 AM 0
Share

i have never done something like this. sorry.

avatar image robertbu · Jul 19, 2013 at 04:49 AM 0
Share

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;
 }
avatar image mattlewis83 · Jul 19, 2013 at 03:17 PM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to cancel OnTriggerExit when already hitting a 2nd trigger 2 Answers

activate gui on trigger enter 1 Answer

Child collider triggering parent collider effects. 1 Answer

multiple collisions to Destroy (gameObject); 1 Answer

How can I make this object destroy on Collison? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges