What is the best and shortest way to group multiple lists of game objects into one list<> of gameobjects,
I add gameobjects to 10 lists by tags, and I need to make a list for collection that contains all of these different gameobjects in the lists
plz any help ?
using System.Linq;
...
var combinedList = list1.Union(list2).Union(list3); // etc...
With duplicates or without?
Using linq you can do this.
Concat will allow you to well, concat item:
var all = ls1.Concat(lst2).ToList();
Union will give you a Union of the lists… which is to mean it’s a distinct collection of the elements from both:
var all = lst1.Union(lst2).ToList();
Make sure you have “using System.Linq;” at the top of your code.
thank you all !!, this this works very well !
Is there a way to add gameobjects by tag, for group
of tags instead of repeating the command for each tag?
something like this maybe?
var tags = new String[] { "tag1", "tag2", "tag3" /*and so on*/ };
var combinedList = new List<GameObject>();
foreach (var tag in tags) {
var objects = FindGameObjectsByTag(tag):
combinedList = combinedList.Concat(objects);
}
thank you ! yes it works fine, but actually it do the same repeating the process for each , i just thought there is a more short or optimized way like searching for couple of tags in same time
something like
gameobjects.AddRange(GameObject.FindGameObjectsWithTag(“ob1”,“ob2”,“ob3”)); ![]()
There’s no “FindGameObjectByAnyTag(…)” that takes in multiple tags, so you’re going to have to loop each tag.
As for making it efficient… well, I’d argue that route already shown is going to be slow since it’s going to loop every GameObject in existence when you do the FindGameObjectsByTag (I assume Unity doesn’t have a look up table for this underneath). So it might be easier to loop over all gameobjects and perform a CompareTag yourself. Of course I’d profile it to see which is actually faster.
In the end though… it’s going to be slow. FindGameObjectsWithTag, and the rest of the “Find” methods are just generally slow and should be expected to be so.
I’m not sure what your end goals are, but when it comes to tags, there’s usually a better way to do it. So might I ask… what is your end goal? What is it you’re attempting to accomplish?
I’m trying to collect groups of game objects in lists and each list will contain gameobjects that have the same behavior , so I made a (game manager script) that manage all these game objects in the lists and apply functions to them separately by their list name
Example :
foreach (GameObject go in gameObjects 1)
{
do1()
do2()
}
foreach (GameObject go in gameObjects 2)
{
do3()
do1()
do4()
}
These lists will be updated continuously to add and remove game objects that have been modified, deleted or added etc.
so i want this update of lists and searching for objects by tag to be as light as possible is there a better way to do this?
Well depending on what do1, do2, do3, do4 is exactly… there can be a few.
One thing is this screams ECS to me, which coincidentally is something @StarManta and I were just telling another user to do on here over in this thread:
When you say you’re using tags to be “as light as possible” because you assume a component is “heavy-weight”? Because yeah, if you’re regularly having to scan the entire scene for all gameobjects with some tag, it’s not exactly “light”.
If you want to just look up gameobjects quickly, you could create a component that attaches to it. And then adds itself to some static pool, and removes itself on disable:
public class TrackedObject : MonoBehaviour
{
public static readonly HashSet<TrackedObject> AllTrackedObjects = new HashSet<TrackedObject>();
void OnEnable()
{
AllTrackedObjects.Add(this);
}
void OnDisable()
{
AllTrackedObjects.Remove(this);
}
}
alternatively you can use Start/OnDestroy if you wanted disabled objects as well.
This of course is just a basic approach, but it’s a quick and dirty way of getting it done, especially when I don’t know your specifics.
If your do1, do2, do3, do4 are actually attempts at efficiently updating large numbers of objects on screen… well, go look into ECS.
Thank you very much, I will read about it. I really need this thing