(SOLVED)GameObject.SetActive cannot active child components

Hi there

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.

Does anyone know what’s happend?

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?

There is an important distinction between the two:
GameObjects: Unity - Manual: GameObjects
Components: Unity - Manual: Introduction to components

2 Likes

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”

Do you mind to provide more details?

Thank you.

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?
1 Like

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.

8727141--1180581--Snipaste_2023-01-13_16-06-18.png

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.

Yes, but only if part1-4 have .activeSelf == true.
i.e. if the checkboxes on those objects are all enabled.

See the images I’ve added. txtConnection will NOT be set Active if the Parent-Object (Connection) is set active, because it itself is not active.

8727171--1180584--1.PNG
8727171--1180587--2.PNG

2 Likes