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 frankyboy450 · Dec 04, 2013 at 10:18 PM · c#gameobjecttags

Making an array that contains object with one of 2 tags

Hi. I'm trying to make an array that contains GameObjects with the tag "RedTeam" or "BlueTeam" I have this code here to make the array

 GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag)

and I was hoping to make it something like this:

 GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag || targetTag2)

But as you probably have guessed, that didn't work either...

If you know a solution to this, please write one ;)

Thanks for reading -Frank

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

2 Replies

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

Answer by clunk47 · Dec 04, 2013 at 10:26 PM

Using Generic Lists and System.Linq works quite well.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class Example : MonoBehaviour 
 {
     string[] tags;
     List<GameObject> gameObjects = new List<GameObject>();
 
     void Start()
     {
         tags = new string[]{"targetTag", "targetTag1"};
         foreach(GameObject go in GameObject.FindObjectsOfType (typeof(GameObject)))
         {
             if(tags.Contains (go.tag))
                 gameObjects.Add (go);
         }
         print (gameObjects.Count);
     }
 }
 
Comment
Add comment · Show 5 · 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 frankyboy450 · Dec 04, 2013 at 10:33 PM 0
Share

And if I'll be calling this function every 10th second or so with 50+ enemies, will it cause loss of performance?

avatar image clunk47 · Dec 04, 2013 at 10:39 PM 1
Share

Not if you're calling every 10 seconds. I just wouldn't call it in Update().

avatar image frankyboy450 · Dec 04, 2013 at 11:09 PM 1
Share

Ok. Won't call it in the Update() ;) Thanks for the help ;)

avatar image clunk47 · Dec 04, 2013 at 11:11 PM 1
Share

If this solution works out for you, don't forget to tick the answer / vote up ;)

Happy Developing

avatar image vexe · Dec 05, 2013 at 06:13 AM 2
Share

This works quite well - I know you wrote this as an example - but as an improvement, I would let the user fill in the tags array from the inspector ins$$anonymous$$d of newing it up in Start.

avatar image
2

Answer by vexe · Dec 05, 2013 at 06:53 AM

Clunk's answer works pretty nice - but just for pure convenience - you could use Linq here as well:

 GameObject[] gos = FindObjectsOfType(typeof(GameObject))
                    .Cast<GameObject>()
                    .Where(g => g.tag == "Bartle" || g.tag == "Doo")
                    .ToArray();

(FindObjectsOfType returns a Object[] hence the Cast())

You could wrap that in a static function:

 public static GameObject[] FindObjectsWithTags(IEnumerable<string> tags)
 {
     return Object.FindObjectsOfType(typeof(GameObject))
             .Cast<GameObject>()
             .Where(go => tags.Contains(go.tag)).ToArray();
 }

Usage:

 string[] tags = new[] { "Bartle", "Doo", "BlackCops", "FlatLine" };
 GameObject[] gos = FindGameObjectsWithTags(tags);

The function takes an IEnumerable - so you could pass it a regular array, a list, an arraylist, etc.

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

19 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

Related Questions

Is there a way to get the actual joystick.Vertical and joystick.Horizontal Value?, 0 Answers

Multiple Cars not working 1 Answer

Change gameobject tag when player collides with another gameobject 1 Answer

Distribute terrain in zones 3 Answers

C# Check If Gameobject is within Collider 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