- Home /
how to hide a GameObject?
I have two cameras----First Person camera and Third Person Camera. First Person camera is inside the model of a GameObject and the Third Person camera follows it. When I switch to the First Person camera, I see part of the model because the camera is inside of it. How can I hide the GameObject when I switch to the First Person camera? Is there some fuction like "GameObject.visible=false" or something else?
Please explain in details as I'm a beginner. Thank you very much!
Answer by GameVortex · Nov 10, 2014 at 08:41 AM
Disabling the GameObject is easy. You can quickly do that with the function SetActive:
gameObject.SetActive(false);
This will however disable all components on the GameObject and any child GameObjects it has. You probably rather want to disable just the renderers. You are not giving a lot of details about your setup, but you are using either MeshRenderer or SkinnedMeshRenderer to render your player.
Search for the appropriate one and disable it by setting its enabled variable to false:
MeshRenderer render = gameObject.GetComponentInChildren<MeshRenderer>();
render.enabled = false;
The airplane model is made by "3ds max" and imported into unity. It's black in unity. Clicked on the model, there is no mesh property.
If you see a model, then there is always either a $$anonymous$$eshRenderer or a Skinned$$anonymous$$eshRenderer. If you do not see it on the gameObject, then it is on a child gameObject ins$$anonymous$$d.
The airplane model is made by "3ds max" and imported into unity. It's black in unity. Why? And how can I colour it?
PS:Clicked on the model, there is no renderers property. Is it need to be added manually?
The renderers are not a property, they are components on the GameObject. The model in the project view is not the gameobject. Your player is most likely a Prefab GameObject.
Alternatively, in 3ds max, you could merge all the objects into one, then on import there would only be one model which you can turn the meshRenderer off for!
Answer by simixd4 · Feb 01, 2021 at 06:31 AM
The airplane model is made by "3ds max" and imported into unity. It's black in unity. Why? And how can I colour it?
Because you don't have material, you need to add material to have a color
Answer by Zejak · Aug 31, 2021 at 08:52 AM
The thing that worked for me was just setting a parent object's scale to zero. This will "hide" any object that is under that parent and you don't have to worry about disabling any components.
But this will not disable the game object itself, which means it will be called, calculated and used no matter that the scale is 0. The child game objects will be called also and all of the components they have.
So if you planning to "hide" for example hundreds or thousands of game objects like this, the game still consume CPU time despite you cannot see them and it is not perfomance-wise solution.
Of course, if you use once in a while it can be acceptable, but probably the implementation behind the code that needs these kind of "hacks" are not planned correctly (no offense, just a constructive criticism).
Your answer
Follow this Question
Related Questions
What is the fastest way to activate or hide/show many gameobjects? 1 Answer
My GameObject isnt showing up in Scene nor in Game 1 Answer
How can I make a gameobject partially invisible by distance? 2 Answers
Hide gameobject until certain condition has been met,Hide an object until a condition is met. 2 Answers
How do I check if a GameObject is visible by a camera? 1 Answer