- Home /
Unity 4 gameObject.active equivalent
The new Unity 4 made some significant changes in how gameObject activation works.
I wanted to know how should we use the new method SetActive and the getters activeSelf and activeInHierarchy to replicate the same behaviour with the old gameObject.active and gameObject.SetActiveRecursively;
example 1:
in the old days we could do this:
`gameObject.SetActiveRecursively(false); gameObject.transform.parent = null;`
Now, we cannot do this as it states that we cannot change the parent of an object and modify its state (active/inactive) in the same frame + the exception "GameObject is already being activated or deactivated."
example 2:
in the old unity I could do this:
`gameObject.SetActiveRecursively(false); gameObject.active = true`
this would keep the children deactivated while the parent is active. Now it seems we cannot do this anymore since all the children reflect the state of the parent as I understood.
Can someone please clarify this for me and say if there is a way to implement the old behaviour with the new gameObject activation philosophy ? Or the only thing left to do is to change everything in the scene and code and adapt to the new changes ?
Answer by Griffo · Oct 08, 2012 at 02:37 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 :)
Answer by Griffo · Oct 08, 2012 at 02:10 PM
gameObject.SetActive(true);
Thanks for the quick reply but that doesn't really help. I know that SetActive(true) is the new SetActiveRecurisively(true). The problem arises with the old use of gameObject.active which has no equivalent instruction in Unity 4.
Plus the fact that you cannot change the parent in the same frame the object is being activated/deactivated