【问题标题】:C# How do I get the GameObject linked to a Transform?C#如何将游戏对象链接到变换?
【发布时间】:2020-05-22 12:27:57
【问题描述】:

我有 Tile 对象和关联的 Tile.cs 文件。这些Tile 对象有一个变量List<Transform> connections 将它们链接到其他Tile 对象。

我想找到connection的连接(绿色圈出),这里有一张图更清楚:

图片链接:https://i.stack.imgur.com/Wwg1k.png

我可以使用该代码获得第一个连接(红色):

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

然后我想要每个t 的连接。我尝试做t.gameObject.connectiont.connection 和其他不起作用的东西,但我不知道该怎么做......

【问题讨论】:

    标签: c# unity3d transform gameobject


    【解决方案1】:

    获取附加到 t 的 tile 组件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-28
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多