为对象的子项指定颜色

时间:2018-04-19 00:32:52

标签: colors children renderer

我所要做的就是将一堆对象的子项分配为随机颜色,但是它似乎只为FIRST子项分配了一种颜色,以下的子项被忽略了,但这很奇怪,因为通过调试,我可以看到这实际上是按预期工作的。

这是代码

    private Color col;
    private Component[] children;

void SetClientColor()
{
    children = GetComponentsInChildren<Renderer>();

    col = new Color(Random.Range(0.1f, 1.0f), Random.Range(0.1f, 1.0f), Random.Range(0.1f, 1.0f));
    if (GetComponentsInChildren<Renderer>() != null)
    {
        foreach (Renderer child in children)
        {
            Debug.Log("assigning color to " + child);
            GetComponentInChildren<Renderer>().material.color = col;
        }
    }
}

Here's basically what happens in game

先谢谢!

0 个答案:

没有答案
相关问题