C# How do I get the GameObject linked to a Transform?

I have Tile objects with an associated Tile.cs file. These Tile objects have a variable List<Transform> connections that links them to other Tile objects.

I want to find the connections of connection (circled in green), here is a picture to be more clear:

link to picture : https://i.stack.imgur.com/Wwg1k.png

I can get the first connection (in red) with that code :

foreach(Transform t in connection)
{
    t.GetComponent<Renderer>().material.color = Color.red;
}

and then I want the connection of each t. I tryed to do t.gameObject.connection, t.connection, and other stuff that didn't work and I can't figure out how to do that...

1 answer

  • answered 2020-05-22 12:47 Panda Strong

    Get a tile component that is attached to t:

    foreach(Transform t in connection)
    {
        t.GetComponent<Renderer>().material.color = Color.red;
    
        var tConnections = t.GetComponent<Tile>().connection;
        foreach(Transform tt in tConnections)
        {
            tt.GetComponent<Renderer>().material.color = Color.red;
        }
    }