How to disable any type of component?
I have a canvas with multiple game objects and I need to disable all the children of one of them, no matter what type it is (Image, text, or game object). I can't use ".SetActive(false)" because it only works for game objects or ".enabled = false" because that doesn't seem to work for game objects. Is there any way to disable something without it's type mattering?
GameObject gobj = canvasSpeakers[g].speaker;
for (int i = 0; i < gobj.transform.childCount; i++)
{
//None of these work
gobj.transform.GetChild(i).enabled = false;
gobj.transform.GetChild(i).SetActive = false;
}
Answer by tatoyoda600 · Jul 26, 2019 at 03:06 AM
This code disables any element whether they are a game object or otherwise
GameObject gobj = canvasSpeakers[g].speaker;
for (int i = 0; i < gobj.transform.childCount; i++)
{
gobj.transform.GetChild(i).gameObject.SetActive(false);
}
Answer by BeanBoy29 · Jul 26, 2019 at 12:17 AM
I think this will wok:
if (gobj is Component)
{
gobj.transform.GetChild(i).enabled = false;
}
else if (gobj is GameObject)
{
gobj.transform.GetChild(i).SetActive = false;
}
oh, and rigidbodys cannot be disabled for some reason, so use this to disable them:
gobj.GetConponet<rigididbody>().is$$anonymous$$inematic = false;
Thank you for the response, but that code doesn't seem to work. However I did manage to find some code that does do what I was looking for. GameObject gobj = canvasSpeakers[g].speaker; for (int i = 0; i < gobj.transform.childCount; i++) { gobj.transform.GetChild(i).gameObject.SetActive(false); }
This code disables any element whether they are a game object or otherwise
Your answer
Follow this Question
Related Questions
How to change the order of things inside of a canvas? 1 Answer
World space canvas child of gameObject 1 Answer
Problems accessing child text from canvas 0 Answers
BCE0044: expecting ''', found '\r'. 1 Answer
Lives going to negative value 3 Answers