I had an issue related with GameObject.SetActive funciton. I intend to use this func to active an deactived gameobject which contains 4 child components during the play mode. However, only the parent object being actived. In my view, this func will recursively active the child components for me.
While if I active this GameObject before enter the play mode, the GameObject.SetActive funciton works well.
SetActive will not activate child objects that are explicitly deactivated with their own checkmarks / active state. It will only activate children which are considered inactive due to the fact they have a deactivated ancestor.
Also your use of the word “components” is a bit confusing here. Are we talking about child GameObjects, or Components attached to a single GameObject?
Sorry for my mistake, the word “components” in my post means the GameObject which had a parent GameObject.
And I did not understand the concept of “It will only activate children which are considered inactive due to the fact they have a deactivated ancestor”
If the object itself is active, but the (grand-)parent is inactive, the object will be inactive in the scene (due to the hierarchy). If you then activate that (grand-)parent, then child will also be considered ‘active’ from that point forward.
This is also why GameObject has 2 different booleans for ‘active’:
activeSelf: Is the checkbox set on this specific object?
activeInHierarchy: is this object considered ‘active’ in the scene?
Thanks for your answer, If that the case, I think I did the right thing. Here is the screen shot of my GameObject hierarchy
the Can3 is an empty gameobject and part1 to part 4 is gameobject with renderer.
What I had done in my code is something like
Can3.SetActive(true);
Theoretically, it should active the part1 to part4 since the parent Can3 had been activated. In my case, only Can3 was activated, the part 1 to part 4 are stay deactivated.