- Home /
Hide but no deactivate a gameobject
Is there any way to hide a GameObject without using setActive? I could disable the render component, but it has many children it's a bit complicated. Any idea?
set the position of the object very far away in the distance somewhere. where it cant be seen.
Changing all children is a "simple" recursive function. You should be able to find a written out example of SetActiveRecursively and modify it to check for and turn on/off a renderer. Unless you have some children who's renderer's should stay off (if you pre-loaded all optional parts.)
But yes, I also often just move them to Vector3.one*999
.
Answer by Veldars · Apr 14, 2015 at 02:48 PM
For me the best way is to disable all renderer as you said but it's not really complicated ^^
foreach (Transform child in transform) {
child.gameObject.GetComponent< Renderer >().enabled = false;
}
GetComponent< Renderer >().enabled = false;
if it's really a huge object make a recurcive
void Start() {
recDisabler(transform);
}
private void recDisabler(Transform t) {
if (t.childCount > 0) {
foreach (Transform child in t) {
recDisabler(child);
}
}
t.gameObject.GetComponent< Renderer >().enabled = false;
}
Yes, I thought about this solution. The problem comes in the case I want to keep a child hiden. I'd have to keep a track of all the status of every children.
If this is the only solution, it's ok. I just wanted to be sure.
Answer by SnStarr · Apr 14, 2015 at 01:42 PM
Change the Alpha Value on the Material its using to 0, this will make it invisible.
But you'd have to go through and do this for every renderer, which is more complicated than just going through and disabling them all.
Plus they need to have a transparent material (or switch to one,) and the system still draws the object each frame. And if some materials had different starting alphas, need to remember what they were in order to restore them.
Answer by Peeling · Feb 10, 2021 at 11:02 PM
Just because I was looking for this and found what I think might be a useful alternative:
public class ActiveButHidden : MonoBehaviour
{
public List<GameObject> targets;
void Start()
{
StartCoroutine(ReEnable());
}
IEnumerator ReEnable()
{
while (true)
{
yield return new WaitForEndOfFrame();
foreach (GameObject go in targets)
{
go.SetActive(true);
}
}
}
private void LateUpdate()
{
foreach (GameObject go in targets)
{
go.SetActive(false);
}
}
}
Drop this on a game object that's always active, and add any game object you want active but hidden into the list of targets. This script will turn them (and their children) off before they are gathered for rendering, and back on before they are gathered for the next round of fixedupdate/update.
CAVEAT:
Any coroutines running on an object are destroyed forever when you make that object inactive, so you can't use this method to hide objects that are running coroutines.
Your answer
Follow this Question
Related Questions
how to toggle on/off a gameobject with a GUI button ? 3 Answers
hide and show node for turret places 1 Answer
Hide/Show a game object and activate/deactivate a script of another game object with one script 1 Answer
How to hide 3D text behind other game objects. 0 Answers
Deactivating the hiding of objects that are out of camera view 1 Answer