• 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 Hogge · Aug 29, 2019 at 02:06 AM · arrayfindgameobjectswithtag

Get parent/root objects in scene?

Hey. I've recently been redoing a lot of my old code to get a more polished game. Among others, I've scrapped my damage colliders and am now using my ragdoll colliders instead. The improvements are many.

However, this has required me to give all components in my enemies the "Enemy" tag.

I have an optimized means of checking if enemies can see the player, by having a child of the Player object get everything with an Enemy tag in the level, stick it into an array and raycast towards one enemy per frame. Which worked perfectly when only the shell object had an "enemy" tag, but now it returns an array consisting of hundreds, if not thousands of entries.

So my question is, how do you modify this:

 ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");

To only find root objects?

Comment

People who like this

0 Show 1
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 Bonfire-Boy · Aug 29, 2019 at 10:50 AM 0
Share

More of an aside than a direct answer to the question, as I think you've got something of an XY problem here.

If you've got thousands of these things, then trying to optimise a searching approach to this is only going to make limited improvements, and before long you'd probably be looking at the same problem again.

Instead I would strongly recommend you move away from using Find and Tags for this. Instead, have an EnemyManager object of some sort that Enemies can register/deregister themselves with, and query that object when you want to know stuff about what enemies there are at any point. This way the work of keeping track is distributed across the times when enemies change, rather than focussed at the point(s) where you need the information.

1 Reply

  • Sort: 
avatar image

Answer by Snyper_Labs · Aug 30, 2019 at 01:41 PM

@Hogge The way you want it sounds overly complicated and can significantly degrade performance.

Here's what I would do: Give the "Enemy" tag only to the parent enemy gameobject. Give it a Capsule Collider (or any collider that fits) and set it as trigger (that way, it won't affect any bullet collision or the ragdoll). Make sure all the ragdoll colliders come inside this trigger collider.

 ActiveEnemies = GameObject.FindGameObjectsWithTag("Enemy");

Now ActiveEnemies will have all the Enemy gameobjects. Here is a function that will return the number of Enemies in front of you within 100 units. (You can change this to raycast all the gameobjects in ActiveEnemies).

 int CheckSight()
 {
     int EnemySight = 0;
     RaycastHit hit;
     if (Physics.Raycast(transform.position, transform.forward, out hit, 100))
     {
         if(hit.collider.gameObject.tag == "Enemy")
             EnemySight++;
     }
     return EnemySight();
 }

Feel free to point out any mistakes.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

127 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 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 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 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 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

finding clones and destroying them doesn't work 0 Answers

Accessing each array members individual variable. 1 Answer

How to check for an empty array? 1 Answer

How to cut off an array of gameobjects? 1 Answer

Tagged objects not returning value 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