Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Le-Capitaine · Mar 24, 2014 at 11:13 PM · c#arraystagsfindgameobjectswithtag

Can a tagged object exclude itself from FindGameObjectWithTag?

This has already been asked once before, but as changing tags isn't an option for me, I'm wondering if any less hacky methods have been figured out since then or, failing that, if anybody could clarify how exactly one can use arrays to do what I'm trying to do; which isn't explained very clearly in the other question.

Comment
Add comment · Show 2
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 Gruffy · Mar 24, 2014 at 11:47 PM 0
Share

Good question. Probably not, no due to it returning a Boolean or operating on it as a string value, both things you wish to keep the same as the rest i imagine.

Possibly through excluding it by referring to "this" during array for loop etc.... if(this.gameObject.tag){array[i].tag = null}- assu$$anonymous$$g the script dealing with array checks is attached to the said object you wish to exclude.

Hope that helps a little. Gruffy

avatar image RyanZimmerman87 · Mar 25, 2014 at 12:26 AM 1
Share

I would probably just set up a bool or integer variable that you can check on the object you don't want to add. That's assu$$anonymous$$g those tagged objects already have a script.

So when you go through your list of all the tagged objects you could just check if that bool or int variable is correct before adding it to the list.

Sounds like you already have a working solution but using a bool or int variable could be a little more flexible if you want more than just one tagged object not to show up in list.

That way you could have a lot more run time flexibility with adding or removing objects from the list but this may be unnecessary for your project.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Simon-Larsen · Mar 25, 2014 at 12:16 AM

Going off of Gruffy's idea. I excluded the gameObject of which this script is attached, but added all the other gameObjects of a certain tag to a List

 using UnityEngine;
 using System.Collections.Generic;
 
 public class FindTaggedGameObjects : MonoBehaviour {
     
     List<GameObject> gos = new List<GameObject>();
 
     void Start () {
         foreach (GameObject go in GameObject.FindGameObjectsWithTag ("YourTag")) {
             if (go.Equals(this.gameObject))
                 continue;
             gos.Add(go);
         }
     }
 }
 
Comment
Add comment · Show 6 · 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 Gruffy · Mar 25, 2014 at 12:20 AM 0
Share

Nice ! Good Answer there @Simon Larsen.

avatar image Lo0NuhtiK · Mar 25, 2014 at 12:22 AM 0
Share

...and here's how to do it with two arrays :

 //get an array of all objects tagged as "Tag"
 GameObject[] all = GameObject.FindGameObjectsWithTag("Tag") ;
 
 //create a new array, 1 less in length since this object won't be included
 GameObject[] others = new GameObject[all.Length-1] ;
 
 //loop thru and assign gameObjects from [all] to [others] so long as it isnt this one
 int index = 0 ;
 for(int i = 0 ; i < all.Length ; i++)
 {
    if(all[i] == this.gameObject)
       continue ;
 
    others[index] = all[i] ;
    index ++ ;
 }
avatar image kat0r · Mar 25, 2014 at 12:43 AM 0
Share

The same as Simons, but with pretty LINQ:

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 
 public class test : $$anonymous$$onoBehaviour 
 {
     List<GameObject> gos = new List<GameObject>();
      
     void Start () 
     {
         gos = GameObject.FindGameObjectsWithTag("YourTag").Where(x => !x.Equals(this.gameObject)).ToList();
     }
 }
avatar image Lo0NuhtiK · Mar 25, 2014 at 12:45 AM 0
Share

ooh... That IS pretty.

avatar image Le-Capitaine · Mar 25, 2014 at 12:54 AM 0
Share

Halle-motherfunking-LLUJAH! This is funking excellent. There should be like an archive of this kind of clever workaround.

Show more comments
avatar image
0

Answer by force5386 · Mar 25, 2014 at 12:44 AM

One idea:

 GameObject[] obj=FindGameObjectsWithTag('Itsyourtagyeah')as GameObject;
 GameObject[] nobj=new GameObject[obj.length-1];
 int i=0;
 foreach(Gameobject o in obj){
 if(o!=this.gameobject){nobj[i]=o; i++;}
 }

It should work.

Comment
Add comment · 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
0

Answer by LukasLicek · Dec 21, 2017 at 04:19 PM

My javascript solution (changing a tag temporarily though) would be :

 var people : GameObject[];
 var person : GameObject;

 function Update(){
     this.gameObject.tag = "Counter"; //temporary tag
     people = GameObject.FindGameObjectsWithTag("Human"); //find everything
     Debug.Log("Found "+people.length+" people.");
     this.gameObject.tag = "Human"; //change your tag back
 }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tagged objects not returning value 0 Answers

Multiple Cars not working 1 Answer

Using a Parameterized arraylist (C#)??? 1 Answer

Distribute terrain in zones 3 Answers

How to make a drag/drop script only affect objects with a tag? 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