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 /
This question was closed Mar 31, 2015 at 11:09 PM by Addyarb for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Addyarb · Aug 30, 2014 at 03:07 AM · multiplayertagsforeach

Find Attached GameObjects in an array using tag?

EDIT: Problem solved, thank you very much guys!

I have a script that I'm trying to edit for multiplayer. Finding any old object with a tag will find every player's objects, so I need a solution to find just the objects attached to the player.

What I'm looking for is a solution like
"Gameobject[] temp = transform.parent.FindWithTags("ActionBars");

But it appears that won't work, because you can't access transforms like that.

So I am guessing I'll need a foreach loop, but I'm terrible with that function.

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

  • Sort: 
avatar image
2
Best Answer

Answer by zharik86 · Aug 30, 2014 at 08:08 AM

Solution like:

  Gameobject[] temp = transform.parent.FindChildrenWithTags("ActionBars");

The simplest way, it to create recurrent function for search of all children in a tree. And when finding necessary to enter it in the list. The code sample is lower:

  private List<Transform> actionbars = new List<Transform>(); //our list for all object with correct tag

  void Start() {
   actionbars.Clear(); //clear list
   this.myFindChild(this.transform, "ActionBars"); //find all objects with tag "ActionBars"
  }

  //recurrent functions
  public void myFindChild(Transform tpParent, string tpTag) {
   for(int i = 0; i < tpParent.childCount; i++) { //See all child and see they child
    if (tpParent.GetChild(i).tag == tpTag) { //check tag of child
     actionbars.Add(tpParent.GetChild(i));
    }
    //See childs of current child
    this.myFindChild(tpParent.GetChild(i), tpTag);
   }
  }

I hope that it will help you.

Comment
Add comment · 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 Addyarb · Aug 30, 2014 at 07:18 PM 0
Share

Thanks so much, that's exactly what I was looking for. Seems way over my head though. Where did you learn this?

I'm now also having trouble with my next line of code that gets each of the spells inside the transform, as it wants a gameObject.

Here's the code I'm working with:

 bool hoverActionbar = false;
         
                         foreach (ActionBars ab in actionbars) {
                                 if (ab.hoverActionBar)
                                         hoverActionbar = true;
                         }
avatar image zharik86 · Aug 30, 2014 at 07:39 PM 0
Share

@Addyarb Where I learned it there it already isn't present:) In my example you find all Transform objects with tag "ActionBars". Then you shall use some other search for your cycle:

  bool hoverActionbar = false;
  foreach (Transform ab in actionbars) {
   if (ab.GetComponent<ActionBars>().hoverActionBar) {
    hoverActionbar = true;
    //If only one variable changes, after its change there is no need to view the list further therefore it is possible to break a cycle
    break;
   }
  }
avatar image Addyarb · Aug 30, 2014 at 08:49 PM 0
Share

Is there any way I can send these scripts for you to look at? I have 3 that reference each other a lot, and it's hard to explain the problem that I'm having, and the character limit is too low to post the scripts. I wouldn't $$anonymous$$d paying you if it's legal to solicit that here! If not I can post the scripts somewhere else and link to them?

avatar image Hathakas · Feb 06, 2015 at 02:24 PM 0
Share

Sorry to bring up an old question...I'm having a hard time figuring out how to access the child. Like the script is finding the child of the parent but how do I access it? I can't set a variable to it. I need to access the child's sprite. (I'm refering to the first answer: private List actionbars = new List();)

avatar image
1

Answer by Testudo · Mar 23, 2015 at 08:44 PM

This worked for me

ActionBars[] bars = transform.Cast().Where (c => c.gameObject.tag == "yourTagName").Select(c=> c.GetComponent()).ToArray ();

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

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Unity networking tutorial? 6 Answers

GameObject.FindGameObjectsWithTag problems 2 Answers

How to have player camera Render enemy but not itself with same tag and camera clipping mask? 0 Answers

How to spawn an animal at each transform of a gameobject with a tag position in another scene? 0 Answers

How to setup a local multiplayer game? 0 Answers


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