Collect List of Transforms based on tag
I've been searching for a while and can't find a single issue similar enough to help.
I have a list of of transforms that I want to start collecting by tag instead of having to assign them all manually using public variables and dragging them in, so I need a way to collect transforms by tags.
public List<Transform> nodes;
void Awake() {
nodes = new List<Transform>();
//look for all objects with "Node" tag and add their transforms to "nodes" list
}
I don't want GameObjects, I want their transforms. I tried
nodes.AddRange(GameObject.FindGameObjectsWithTag("Node").transform);
but it said "UnityEngine.GameObject[] does not contain a definition for transform".
I assume this means I'm trying to change the list instead of each individual entry, but I also couldn't find to do that.
All I want is to end up with a "List" filled with transforms of all objects with the "Node" tag, preferably in a single line of code. Any help?
Answer by Ahndrakhul · Jan 09, 2017 at 09:17 PM
This isn't one line, but it's a bit shorter.
List<Transform> nodes;
void Awake()
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Node"))
{
nodes.Add(go.GetComponent<Transform>());
}
}
This is what I was looking for, thank you! No worries that an extra line needs to be taken for the foreach loop, that's understandable; my creation of a whole new List and many more lines was not.
I'll accept this as the answer if you put it into a complete reply.
is there a way to update this? so if the tag has been removed it removes it from the array and also if a new object has been taged
Answer by Landern · Jan 09, 2017 at 09:02 PM
FindGameObjectsWithTag returns an array, an array of GameObject doesn't have a transform field/property because it has many GameObject and each of them have a transform.
You need to get each transfrom from the array, use linq for a quickie.
This isn't tested, also you might want to put the AddRange into a Start method so all gameobject Awake methods are called.
nodes.AddRange(GameObject.FindGameObjectsWithTag("Node").Select(go => go.transform));
something like that, need to use System.Linq in the class. Code untested, but you should know what the issue is now.
I've already tried .Select() as well, but I'll try putting into Start and see if that helps.
Nope, error ensues: "System.Array" does not contain a definition for "Select" etc.
Again, if you don't put:
using System.Linq;
At the top of your class then the extension method will not exist in the context of the class. Select is part of the Linq extension methods.
Being in Awake or Start will not affect whether the function from a separate namespace exists or not.
Oh sorry, I'm not super familiar with namespaces so my $$anonymous$$d skipped over that bit, something I need to work on. I'll try this again.
Ah works now, thanks :D
Answer by JamesXyXz · Sep 10, 2021 at 09:48 PM
foreach (GameObject go in GameObject.FindGameObjectsWithTag("CheckpointRoute01"))
{
RoutePositions01.Add(go.GetComponent<Transform>());
}
This works fine for me, when i test it in the Unity Editor, but when i export it to Windows (x86-64), then the sequence messed up. Why and how can i fix it?
Your answer
Follow this Question
Related Questions
transform.parent.tag not working? 1 Answer
transform.parent not working? (Unity 2d) 1 Answer
Need assistance with an primitive AI. 0 Answers
How to find nearby objects? 2 Answers