- Home /
have an array that finds game objects with tag that is the root object/highest parent
I want to create an array that searches for all game objects with the tag player but it only adds the game objects to the array that are a root objects/highest parent
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
Answer by Malace · Apr 24, 2018 at 07:37 PM
I'm not 100% sure why you are getting children on your
array[] = gameobject.findobjectswithtag('player');
.. To me it sounds like over usage of the player tag. As I can't think of a reason to tag the children as player.
By default the children of your player object shouldn't be tagged. At least not with the player tag. Anything below the root object should have a custom tag, if any. So 'playerGun' or whatever the child object is would be fine. If for some reason you need change them for all players.
GameObject.Find is a intensive function. It eats quite a bit of resources to check every game object for whatever you search for. A better way to track players is to add them to your array at creation. Either when the game starts and instantiates the object. Or later when a new player joins. I find I typically only reference tags when checking collisions.
I'm not sure if any of that helps. Again without more context I'm just providing what I can.