Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Adding objects to list or using gameobjectswithtag which is better

Discussion in 'Scripting' started by kader1081, Aug 28, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    Hi, which one is more performance efficient, adding object to a list instead of using gameobjectswithtag. Let's say i have enemies, friends, buildings etc. Instead of using gameobjectswithtag i am adding these to a list enemies to enemies list then removing them ondestroy is this more performance efficient
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    While you should always look at the profiler, generally speaking it's better to maintain your own references. So, when instantiating enemies for example, just put them into a list as you're doing.
     
  3. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    i add and remove like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class getObjects : MonoBehaviour
    6. {
    7.     public static List<GameObject> placedAmmos = new List<GameObject>();
    8.     public static List<GameObject> unPlacedAmmos = new List<GameObject>();
    9.     public static List<GameObject> workPlaces = new List<GameObject>();
    10.     public static List<GameObject> turrets = new List<GameObject>();
    11.     public static List<GameObject> beavers = new List<GameObject>();
    12.     public static List<GameObject> enemies = new List<GameObject>();
    13.     public static List<GameObject> armoredEnemies = new List<GameObject>();
    14.     public static List<GameObject> flyingEnemies = new List<GameObject>();
    15.     public static List<GameObject> seaEnemies = new List<GameObject>();
    16.     public static List<GameObject> allFriends = new List<GameObject>();
    17.     public static List<GameObject> allEnemies = new List<GameObject>();
    18.  
    19.     public static void addObjectTolist(List<GameObject> list, GameObject obj)
    20.     {
    21.         list.Add(obj);
    22.     }
    23.     public static void removeObjectFromList(List<GameObject> list,GameObject obj)
    24.     {
    25.      
    26.        list.Remove(obj);
    27.            
    28.     }
    29. }
    30.  
    and this is the script that i am storing objects.
    Code (CSharp):
    1.   void Start()
    2.     {
    3.         source = GetComponent<AudioSource>();
    4.         getObjects.addObjectTolist(getObjects.beavers, gameObject);
    5.         getObjects.addObjectTolist(getObjects.allFriends, gameObject);
    6.     }
    7.     private void OnDestroy()
    8.     {
    9.         getObjects.removeObjectFromList(getObjects.beavers, gameObject);
    10.         getObjects.removeObjectFromList(getObjects.allFriends, gameObject);
    11.     }
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    One thing I can recommend, is never make Lists of the gameObjects, make a list of their classes. As you will find when messing with those(gameObject) lists you'll constantly have to GetComponent(). But if you have lists of those components, you'd never need to "get" them, you'd already have them.

    And also, when iterating through gameObjects you technically load each gameObject, which is slow and an un-needed performance loss. As you'll find iterating through things like colliders or classes is way quicker.

    Then you might ask, well what if I have different scripts but want them in the same list? easy, use Inheritance and make a parent class that is the sum of those sub-classes.
    Code (CSharp):
    1. public class Animals : MonoBehaviour { }
    2.  
    3. public class Beaver : Animals { }
    4.  
    5. public class Bear : Animals { }
    Then your
    List<Animals> allAnimals;
    can have all the bears or beavers it wants. :)