- Home /
Unity4 .Active replacement
I am upgrading to unity4 from 3.5 and I am getting warnings from the changed behavior on the .Active property of GameObjects. The replacement function, SetActive(), affects the child GameObjects as well.
Is there no replacement for toggling a single GameObject?
I'm not using Unity 4 yet, but are you sure that SetActive affects the children? To affect them shouldn't be used "SetActiveRecursively"?
From the release notes : We have changed how the active state of GameObjects is handled. GameObjects active state will now affect child GameObjects, so setting a GameObject to inactive will now turn the entire sub-hierarchy inactive. This may change the behavior of your projects. GameObject.active and GameObject.SetActiveRecursively() have been deprecated. Ins$$anonymous$$d, you should now use the GameObject.activeSelf and GameObject.activeInHierarchy getters and the GameObject.SetActive() method.
Currently, .Active and SetActiveRecursively still work, but I am trying to prepare for when they don't
Sounds like the GameObject.activeSelf is the new active and GameObject.activeInHierarchy is the new GameObject.SetActiveRecursively, right?
Both of those are ReadOnly members. SetActive is the only replacement I could find. It has the affect of triggering all children as well.
Sorry man, I'm out out alternatives. $$anonymous$$aybe it's the new behavior, with no workarounds. When I think about it it makes sense to me, I think i never wanted to deactivate an object and keep his children running.
Answer by mcroswell · Nov 09, 2012 at 03:49 AM
It's really a paradigm shift, based on the fact that if you have a parent GO then if you turn it on, then the children that are on should also come on. If the children are off then they will stay off unless you explicitly change the children's state.
So, if the parent's off then the children are active. But, if the parent is active, the children may or may not be off.
Example code (requires a test GO and two parented children GO trees) is probably the best way to see this.
public GameObject testGO;
public GameObject GO_1;
public GameObject GO_1_1;
public GameObject GO_1_2;
void Start () {
PrintStates();
}
void PrintStates()
{
PrintActiveSelf();
PrintActiveInHierarchy();
}
void PrintActiveSelf()
{
print ("ActiveSelf for testGO=" + testGO.activeSelf
+ " GO_1=" + GO_1.activeSelf
+ " GO_1_1=" + GO_1_1.activeSelf
+ " GO_2_1=" + GO_1_2.activeSelf );
}
void PrintActiveInHierarchy()
{
print ("ActiveInHierarchy for testGO=" + testGO.activeInHierarchy
+ " GO_1=" + GO_1.activeInHierarchy
+ " GO_1_1=" + GO_1_1.activeInHierarchy
+ " GO_2_1=" + GO_1_2.activeInHierarchy );
}
void OnGUI () {
if (GUI.Button(new Rect(Screen.width/2, Screen.height/2-50, 200, 40), "Toggle just GO_1"))
{
GO_1.SetActive(!GO_1.activeSelf);
PrintStates();
}
if (GUI.Button(new Rect(Screen.width/2, Screen.height/2, 200, 40), "Toggle just GO_1_1"))
{
GO_1_1.SetActive(!GO_1_1.activeSelf);
PrintStates();
}
if (GUI.Button(new Rect(Screen.width/2, Screen.height/2+50, 200, 40), "Toggle just GO_1_2"))
{
GO_1_2.SetActive(!GO_1_2.activeSelf);
PrintStates();
}
}
}
I would have attached unitypackage but Answers apparently does not allow uploading of that data type attachment. So, here's a screen shot of the Hierarchy and Properties:
So, once you've set up the script, run it, press the buttons, look at the console. You'll notice that you can have a child that can have activeInHierarchy false, while still having activeSelf true. This is the key to understanding this: The child's activeSelf will not be changed by the parent, but the activeInHierarchy will.
Now, if you have to recursively set your children components you can write your own code to do that. I wish Unity would have left SetActiveRecursively() in, but I'm pretty sure I don't need it based on the new paradigm. What do you all think?
Thank you $$anonymous$$ichael, after long hours of frustration, I figured all of this out myself. It's actually pretty cool and very intuitive after you get rid of the old paradigm. It's very nice that in the editor you are not asked anymore to activate all children or just the parent. I hope this will also boost performance.
I agree Paul, it does seem intuitive now, and really is very cool. By the way, it was this original Question and your Answer and earlier insight (Oct 8) that made me realize it was a bit deeper situation than I first realized. I originally assumed it was just a recursive replacement, and came to Answers to find out I underestimated the situation.
I would guess, that it does improve performance too, because internally the engine can short-circuit its tree iteration when sending event messages, rendering and such to the children if the parent is off. Then when it is toggled, the engine doesn't even have to do the work of toggling the children's flags.
Answer by paulsz · Oct 08, 2012 at 02:44 PM
So basically, there is no workaround to keep the same structure and code from the pre Unity 4. We just have to change and adapt the structure of the game objects in the scene and the code to adapt to the new game activation strategy. Now I can start modifying without worrying that there is an easier way :)
I was hoping someone at Unity would confirm. Can't say I am excited about this change...
Same here, I have to change alot now and I'm not even sure how can I replicate the old behaviour :((
Is the GameObhect.SetActive() soppose to leave all the gameobject scomponent's active??
Is a real shame that Unity won't comment. This problem will mean that many developers who would want to do a Windows 8 phone version (when it arrives) will really struggle to convert over. I'm sure $$anonymous$$icrosoft are not too pleased with this too. I agree it is better this way, but you can't just change such a key system in such a mature product.
Why would $$anonymous$$icrosoft care at all? And yes, if there's a good enough reason, then you can change any system, "key" or not. It's better to fix behavior that needs to be fixed rather than leave a sub-optimal solution in place just because "it's always been that way".
Your answer
![](https://koobas.hobune.stream/wayback/20220613081555im_/https://answers.unity.com/themes/thub/images/avi.jpg)