Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
Add comment · 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

$$anonymous$$ore 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.

Ins$$anonymous$$d I would strongly recommend you move away from using Find and Tags for this. Ins$$anonymous$$d, have an Enemy$$anonymous$$anager 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

· Add your reply
  • Sort: 
avatar image
0

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

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

store GameObject.FindGameObjectsWithTag in an array 2 Answers

FindGameobjectsWithTag -> Array 2 Answers

AddComponent To GameObjects On Array 1 Answer

Check distances of multiple objects, and do something if all outside of range 2 Answers

finding clones and destroying them doesn't work 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