how to make my 3D Gameobject invisible if no renderer attached in UNITY?

醉酒当歌 提交于 2019-12-24 07:47:06

问题


I have 3D object and I want to make it invisible by turning off its rendering. But when I run the code in runtime it shows ::
" Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> UnityEngine.MissingComponentException: There is no 'Renderer' attached to the "3dObject" game object, but a script is trying to access it."
I want to know if There is no renderer then how I am able to see that object in scene/game view. and also in inspector nothing is there (mesh renderer / renderer / mesh filter).
I'm Using c# : this.GetComponent<Renderer>().enabled=true;
I'm using vuforia and want to keep last tracked object on screen after Tracking Lost function is called.
OR Is there any other way to do this...?
I'm new in unity and I know its a pretty basic question but not getting anything. Need Help..


回答1:


One approach could be:

  1. Create a new layer called: InvisibleGO.
  2. Set the layer of your GameObject to be InvisibleGO
  3. Then you can set the culling mask for the camera on each camera to only display the layers that you want:

https://docs.unity3d.com/Manual/class-Camera.html



来源:https://stackoverflow.com/questions/44093738/how-to-make-my-3d-gameobject-invisible-if-no-renderer-attached-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!