Search Unity

FindGameObjectsWithTag efficiency

Discussion in 'Scripting' started by NCarter, Dec 30, 2006.

  1. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Out of curiosity....

    I just made a system by which every object of a class adds itself to a static Arraylist when it gets created, and removes itself in OnDisable. I then iterate through that array to find objects which are within a certain distance of a given point.

    After writing this code, it occurred to me that GameObject.FindGameObjectsWithTag() might have been simpler and more efficient. My initial thought was that it would be more expensive to (frequently) search through a complex hierarchy than to simply iterate over objects which you already know are relevant. However, it occurred to me that Unity might already put every object of a given tag into a list, so it can return those objects without searching for them. That would obviously make my piece of code redundant.

    I wonder if OTEE could shed some light on how this kind of thing is implemented in Unity?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    At the moment Unity keeps a list of all game objects which have anything else than "Untagged" as their tag. We go through this list to find the game objects by their tag. Usually the list of tagged game objects is quite small, so i would presume in most cases this is not a performance issue. Keeping an ArrayList of exactly the objects you need is probably more efficient though.

    If you are looking for the absolute maximum efficiency the best way would be to make a builtin array (int[]) of structs holding position and reference to the object and some of the most important data you need to keep around. By keeping it in a builtin array, you can iterate through all objects nicely aligned in memory, without trashing the cache. Since you have the position cached as well, you don't need to call any functions to calculate it thus saving cycles. The downside is of course that you need to synchronize positions and maintain the builtin list. Depends on how many enemies you want to have.
     
    bowserscastle likes this.
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Thanks, that's very helpful!

    That sounds good.

    Out of interest, from your point of view, would there be any benefit to putting different tags into different containers? It occurs to me that it might be the case that literally every object has a tag, if that's how the developer designed their game.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I think it depends on how you are using tags. In the projects i have worked on or seen when people send them in, there are usually very few tagged objects. In a large game maybe 50. Theoretically it could make a difference, practically i've never seen it show up in a profile. Usually we don't do premature optimization.
    Profile first. Build a standalone player (unstripped) run the game with lots of enemies and run Shark on it.