Custom Isolation Script - Objects take forever to turn back on

Hello,

I’m currently working on a simple script which will isolate the objects I have selected in the editor. The isolation system is fine and works very quickly, even in larger scenes. The problem I’m running into is when I go to turn everything that wasn’t isolated back on.

The system is fairly intelligent, only turning off the highest hierarchy level of each group of objects:

Walls
Wall 1
Wall 2
Wall 3
Doors
Door 1 Hinge
Door 1 panel
Door 2 Hinge
Door 2 panel
Door 3 Hinge
Door 3 panel

Given the hierarchy setup above, let’s say that I have selected “Door 1 panel.” When I run my isolation script, it will turn off “Walls”, but leave each of it’s children on, and then it will also turn off “Door 2 Hinge” and “Door 3 Hinge”, while leaving “Doors” and “Door 1 hinge” on. It is set up this way to limit the number of objects which are actually turned off, the goal being that the fewer objects that I turned off, the fewer objects I would have to turn back on, and the less time it will take to turn everything back on.

Unfortunately, this has not proven to be the case. I am testing this on a building I worked on and, while it’s really quick to isolate my selection (it takes less than two seconds to work through everything and turn it off, not bad for a nine-story hotel), it takes forever to turn everything back on. (Forever, in this case, being approximately fifteen minutes) It is taking this long even when only 27 objects actually need to have their SetActive() method called.

I am trying to figure out why it is taking so long. When the script isolates, it has to cycle through every object in the hierarchy to check it and its children for selected objects, turning objects off and adding them to a list of hidden objects as it goes. When I restore objects, all it’s having to do it cycle through the hidden objects list, turning things back on… it is very confusing >_>

Here’s the important code:

Once it’s determined that an object in the hierarchy isn’t a selected object, and doesn’t contain a selected object as a child, it is turned off with this code:

if(!checkIsChild(currentObject, selectedObjects)){
	currentObject.SetActive(false);
	hiddenObjects.Add (currentObject);
	continue;
}

Once the restore menu option is selected, it calls the restoration method:

	public void resetIsolation(){
		//Make sure that hiddenObjects isn't null
		if(hiddenObjects == null || hiddenObjects.Count==0){
			return;
		}

		StartCoroutine (restoreLevels());
	}

	private IEnumerator restoreLevels(){

		foreach (GameObject o in hiddenObjects) {
			if(o==null){
				continue;
			}

			o.SetActive(true);
		}

		yield return new WaitForSeconds (0.01f);
		hiddenObjects.Clear();
	}

It really does seem like setActive(true) just takes way, way longer that setActive(false)… Is this the case, and, if so, does anyone know of another way I could approach this? I know that turning off mesh renderer is simpler and faster, but not all objects have one, so it wouldn’t be able to catch every object in the scene; additionally, when I’m importing my fbx’s they don’t have mesh renderer, only their children do, so I wouldn’t be able to use the intelligent system I designed to limit the number of objects being turned off.

Thank you in advance for any help. If you need to see more of the code I can provide it, but I’d prefer not to since I did spend a decent bit of time figure out exactly how to go about this, and intend to include it as part of an asset package I am developing.

For anyone who happens across this thread with a similar issue, I’m sorry to say that I do not have an answer for you. I decided to just rewrite the script to isolate objects by turning the MeshRenderer on and off. It works fast enough, there’s barely any additional lag from my other system when turning them off, and it turns them back on in the same amount of time it takes to turn them off.

I have no clue why SetActive(true) takes so much longer than SetActive(false), but apparently it does… so… yeah.

SetActive(true) takes longer than SetActive(false) because it re-initializes a bunch of things - internally it’s creating PhysX representations of colliders, initializing buffers for audio sources, etc.

That makes sense. Thank you for the elaboration.