1
\$\begingroup\$

I have the following line of code:

GameObject.Find("_obstacles").SetActive(false);

It serves to leave an inactive object, but the children of this object are not getting with the same status.

My result is:

enter image description here

The objects children do not inherit the parent object state (as stated in the > documentation of Unity)!

But the obsolete method "SetActiveRecursively()" works for me! O.o The Very strange, no?

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Have you tested if the child objects really are active in game? The behavior has changed from Unity3 when SetActiveRecursively was needed. Since Unity4, setting the parent to inactive should also deactivate the child objects (in game) even though it still shows them as active in the inspector.

See the documentation for GameObject.SetActive()

Note that a GameObject may be inactive because a parent is not active. In that case, calling SetActive() will not activate it, but only set the local state of the GameObject, which can be checked using GameObject.activeSelf. This state will then be used once all parents are active.

If you really do want to deactivate the child objects, even when the parent is reactivated, then you will need your own function. Unity doesn't provide it anymore.

Here is a link to a solution that you can copy-paste to your own code.

\$\endgroup\$
  • \$\begingroup\$ Thanks @Fuzzy Logic But i need set active false children's too! Not "only set the local state of the GameObject"... I have to make a way for this? \$\endgroup\$ – Igor Sep 14 '15 at 5:51
  • \$\begingroup\$ Yes. If you specifically want the children to also be deactivated (even after you reactivate the parent), then you will need your own function. I've updated my answer. \$\endgroup\$ – Fuzzy Logic Sep 14 '15 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.