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?

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.

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>());
        }
    }

foreach (GameObject go in GameObject.FindGameObjectsWithTag(“CheckpointRoute01”))
{
RoutePositions01.Add(go.GetComponent());
}

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?