- Home /
Unity 4 SetActive - SetActiveRecursively parent-child trouble
Hello,
Now since setActiveRecursively obsolete in Unity4 I'm trying to change all lines I used after upgrading the project.
I have a problem with cases like below How do you do the same job? this.gameObject.SetActiveRecursively(false); this.gameObject.active = true;
Actording to Unity 3.5 to 4 guideline they say "setting GameObject.active is equivalent to calling GameObject.SetActive(). Calling GameObject.SetActiveRecursively() is equivalent to calling GameObject.SetActive() on the GameObject and all of it's children."
So from looking here it should be this.gameObject.SetActive(false); this.gameObject.SetActive(true); which is nonsense
Should I loop all children and make them SetActive false? -- again I think nonsense..
So if you have any idea or solution please share Cheers
I personally used SetActive, ins$$anonymous$$d of SetActiveRecursively and it Enabled/Disabled all the children objects too. Don't loop, just use SetActive(false) to the parent.
Don't think you got what I meant. If you don't loop than how can you only activate the parent and let all children stay deactive. It's not possible...
Answer by hoy_smallfry · Feb 26, 2013 at 04:15 PM
I'd say you should loop through all the immediate children and call SetActive() on them. Immediate children are the ones directly connected to the parent, so if you turn them inactive, should achieve the same effect. Yes, its a little ridiculous, but so was having to call gameObject.SetActiveRecursively(false); gameObject.active = true;
.
To modify just the immediate children:
Javascript:
for (var child : Transform in transform)
{
child.gameObject.SetActive(false);
}
C#:
foreach (Transform child in transform)
{
child.gameObject.SetActive(false);
}
Thank you.. Seems like that's the only way for now. Still not sure why Unity made that change in the first place. Doing SetActive manually probably costs more performance than using SetActiveRecursively..
Answer by Xaon · Aug 20, 2013 at 04:38 PM
There are gameObject.activeSelf and gameObject.activeInHierarchy. I haven't used those yet but they might solve similar problems. Here is the big explanation for those who are migrating from 3.5 version: http://docs.unity3d.com/Documentation/Manual/UpgradeGuide3540.html
Your answer
Follow this Question
Related Questions
SetActiveRecursively(true) replacement in Uinity4. 2 Answers
SetActiveRecursively not activating children 2 Answers
gameObject.SetActiveRecursively 1 Answer
Activating gameObject issue... 1 Answer