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?

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:

4817-setactivetest_properties.png

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?

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 :slight_smile: