Search code examples
c#unity-game-enginevuforiarenderer

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


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.. enter image description here


Solution

  • 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