Unity3d 没有';渲染器&x27;附于「;机器人2“;游戏对象,但脚本正在尝试访问它

Unity3d 没有';渲染器&x27;附于「;机器人2“;游戏对象,但脚本正在尝试访问它,unity3d,Unity3d,MissingComponentException:没有“渲染器”附加到 “机器人2”游戏对象,但脚本正在尝试访问它。你 可能需要向游戏对象“robot2”添加渲染器。或者你的 脚本在使用之前需要检查组件是否已连接。 UnityEngine.Renderer.get_材质()(位于 :0)ColorChange.Start()(在 资产/颜色变化。cs:21) 我的Unity项目中有一个fbx机器人2,它已经作为资产导入。我想在程序启动时更改颜色,但收到此消息。我如何在Unity中呈现我的fbx

MissingComponentException:没有“渲染器”附加到 “机器人2”游戏对象,但脚本正在尝试访问它。你 可能需要向游戏对象“robot2”添加渲染器。或者你的 脚本在使用之前需要检查组件是否已连接。 UnityEngine.Renderer.get_材质()(位于 :0)ColorChange.Start()(在 资产/颜色变化。cs:21)

我的Unity项目中有一个fbx机器人2,它已经作为资产导入。我想在程序启动时更改颜色,但收到此消息。我如何在Unity中呈现我的fbx

公共颜色colorStart=Color.red;
公共颜色colorEnd=Color.green;
公开渲染;
//在第一帧更新之前调用Start
void Start()
{
rend=GetComponent();
rend.material.color=colorStart;
}

基本上,错误消息说明了一切:

仅返回附加到与脚本相同的游戏对象的组件

但是您的
robo2
没有网格,因此也没有
渲染器


在本例中,您更希望使用它返回脚本所附加到的
游戏对象
本身的所有相应组件或递归嵌套在其下的任何子对象

void Start()
{
    // pass true in order to also include disabled or inactive child Renderer
    foreach(var rend in GetComponentsInChildren<Renderer>(true))
    {
        rend.material.color = colorStart;
    }
}
void Start()
{
//传递true,以便还包括禁用或不活动的子渲染器
foreach(GetComponentsInChildren中的变量rend(true))
{
rend.material.color=colorStart;
}
}

Robot2未连接渲染器组件。看起来有些子游戏对象可能有渲染器。GetComponent()中不包括子游戏对象;查找。如何渲染和更改儿童的颜色?汽车是什么?@lilymas a typo^^我在我的智能手机上写了这个,非常抱歉;)我注意到另一个问题,我的自动更正删除了
s
。。它应该是
GetComponentsInChildren
,以便获取所有而不仅仅是第一个
void Start()
{
    // pass true in order to also include disabled or inactive child Renderer
    foreach(var rend in GetComponentsInChildren<Renderer>(true))
    {
        rend.material.color = colorStart;
    }
}