解决Unity3D中“There is no 'Renderer' attached to the game object”问题
2015-10-19 13:48
1481 查看
最近在开发一个Unity3D的项目,遇到一个问题,弄了老半天才解决,过程中发现中文的资料并不多(也可能是我的搜索能力问题),于是写一篇博客,可能能帮助到以后碰到这个问题的开发者们。
问题描述一:
我遇到的情况是,将自制的prefab拖入场景,拖入多个相同prefab后,除了第一个prefab,别的都无法用renderer.material访问到材质,错误提示为“There
is no 'Renderer' attached to the "XXXobj" game object, but a script is trying to access it.You probably need to add a Renderer to the game object "XXXobj". Or
your script needs to check if the component is attached before using it.”
这个问题在网上遇到的人很多,解决方案也很多,不过对于初学者比如我,一个个尝试基本都是无效的,可能在此我也只是又提出一个解决方案。
在Google搜到一个回帖,如下。遇到该错误提示的U3D开发者都可以试试看这个解决方案。原文:http://forum.unity3d.com/threads/how-to-call-getcomponents-from-an-instantiate-object.21799/
“But
since PlayerMesh is part of remotePlayerPrefab, how is GetComponent going to know which PlayerMesh to get?
Here is a breakdown of what is all going on.
From the PreFab of remotePlayerPrefab, there is are 2 gameObjects attached. One is "PlayerMesh" and the other is "Root".
"Root" has the Skinned Mesh Renderer attached. The purpose of calling the "RemotePlayerColor" (sorry, I spelled it incorrect last time) is to change the material of
the Skinned Mesh Renderer by using the "renderer.material.color = Color.red;" for example. But the "RemoteCharacterColor" has a listing of materials (about 16) and I just change the index number such as "renderer.sharedMaterial = material[index];"
If there is an different way to change the material of a PreFab or if moving the "RemotePlayerColor" to the PreFab and changing the Skinned Mesh Renderer dynamically,
I'm all for it.”——By matrix211v1
也就是说,对于两个相同的prefab,只有一个是所谓的Root,另一个的材质是共享了Root的材质,因为复制对象没有自己独立的材质,所以会访问不到,对于这种情况,需要使用sharedMaterial来访问材质。
另外,使用material来改变材质,本质上是u3d需要克隆一个材质,再更改,再替换,而sharedMaterial则是更改本来的材质,效率上,也应该使用sharedMaterial。
问题描述二:
在脚本中使用Resource.Load(),并Instantiate动态载入的prefab,无法使用GetComponent获取到组件比如Renderer的问题。
解决方法是,使用GetComponentInChildren来获取相应组件。
原理是这样的,在U3D中,动态载入的prefab可能会(不一定是所有,所以说可能)被挂在一个U3D自动动态创建的空对象下,所以它的Renderer不一定会在顶层,所以需要用InChildren来访问到。
问题描述一:
我遇到的情况是,将自制的prefab拖入场景,拖入多个相同prefab后,除了第一个prefab,别的都无法用renderer.material访问到材质,错误提示为“There
is no 'Renderer' attached to the "XXXobj" game object, but a script is trying to access it.You probably need to add a Renderer to the game object "XXXobj". Or
your script needs to check if the component is attached before using it.”
这个问题在网上遇到的人很多,解决方案也很多,不过对于初学者比如我,一个个尝试基本都是无效的,可能在此我也只是又提出一个解决方案。
在Google搜到一个回帖,如下。遇到该错误提示的U3D开发者都可以试试看这个解决方案。原文:http://forum.unity3d.com/threads/how-to-call-getcomponents-from-an-instantiate-object.21799/
“But
since PlayerMesh is part of remotePlayerPrefab, how is GetComponent going to know which PlayerMesh to get?
Here is a breakdown of what is all going on.
From the PreFab of remotePlayerPrefab, there is are 2 gameObjects attached. One is "PlayerMesh" and the other is "Root".
"Root" has the Skinned Mesh Renderer attached. The purpose of calling the "RemotePlayerColor" (sorry, I spelled it incorrect last time) is to change the material of
the Skinned Mesh Renderer by using the "renderer.material.color = Color.red;" for example. But the "RemoteCharacterColor" has a listing of materials (about 16) and I just change the index number such as "renderer.sharedMaterial = material[index];"
If there is an different way to change the material of a PreFab or if moving the "RemotePlayerColor" to the PreFab and changing the Skinned Mesh Renderer dynamically,
I'm all for it.”——By matrix211v1
也就是说,对于两个相同的prefab,只有一个是所谓的Root,另一个的材质是共享了Root的材质,因为复制对象没有自己独立的材质,所以会访问不到,对于这种情况,需要使用sharedMaterial来访问材质。
另外,使用material来改变材质,本质上是u3d需要克隆一个材质,再更改,再替换,而sharedMaterial则是更改本来的材质,效率上,也应该使用sharedMaterial。
问题描述二:
在脚本中使用Resource.Load(),并Instantiate动态载入的prefab,无法使用GetComponent获取到组件比如Renderer的问题。
解决方法是,使用GetComponentInChildren来获取相应组件。
原理是这样的,在U3D中,动态载入的prefab可能会(不一定是所有,所以说可能)被挂在一个U3D自动动态创建的空对象下,所以它的Renderer不一定会在顶层,所以需要用InChildren来访问到。
相关文章推荐
- Kinect结合Unity3D引擎开发体感游戏(一)
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Unity3D游戏引擎实现在Android中打开WebView的实例
- unity3d调用手机或电脑摄像头
- 分享一个开源的网络游戏服务器架构—HouHai
- Unity3D插件详细评测及教学下载
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3d TransformPoint 函数研究
- Unity3d 动态加载fbx模型文件
- unity3d地形系统总结