Performance of SetActive on object with many children

Essentially my question is if
When I deactivate an object is unity iterating through its children and deactivating each of them individually on the background?
or is it a cleaner operation than running a loop?

Basically what I have is a performance issue:

My goal is to deactivate a lot of objects consuming the least possible cpu.

I can iterate through a list of all objects I want to deactivate or;

Deactivate a parent object which deactivates all the children.

Based on this i can decide if i SetParent when I instantiate objects and then deactivate the parent, or if I add them to a list and then loop through the list to deactivate( this saves me the setparent overhead but forces me to iterate if deactivating the parent is faster. )

consider a lot of children, like 1k, 10k or 100k or 1M

I dont know for sure, but given that you can deactivate a parent and then activate a single child again (Edit: this example is not true as pointed out below!), i’m reasonably sure it’s iterating through all of them and deactivating them one by one.
In normal cases this should be pretty fast.

However, if you plan on deactivating 10k, 100k or 1m gameobjects, this will undoubtedly be slow. But that wont be your biggest problem by a long shot. Having 100k or so gameobjects alone will cripple your performance. There is some overhead in having gameobjects, especially if you plan on having one or more Monobehaviors on them which have an Update function. At that point, forget about the idea.
If you explained why you need this, we may be able to find another solution. The newly introduced DOTS, for example, can easily handle 100k+ entities if that’s what you want. For a million it depends on how complex the entities are and whether or not you want to render them (as it’s more likely to be bottlenecked by the graphics card at that point).
More information is neede here.

thank you, the objects will have just 1 sprite renderer

I just wanted to know how unity was handling the deactivation, to know if set active is the same thing as a 10k loop, if I understand the mechanics i can go from there

I don’t believe Unity loops through them and actually sets them to inactive, since each object retains its current “active” status. However, you can easily test the performance impact of setting an object with children to inactive/active using the profiler. It’s not necessarily easy to tell how this sort of thing is handled on the backside of things because we’re not able to look at the code for it.

hmm so maybe set active is a n(1) operation?

You can what now?
If I do this:

public Transform someChild;
void Start() {
someChild.parent.gameObject.SetActive(false);
someChild.gameObject.SetActive(true);
}

someChild will not be activated. So what are you talking about?

Way way back (like Unity 1.x) this is how activating GameObjects worked, but it’s literally been over a decade since then.

As far as the actual question is concerned: Yes, there is definitely going to be a per-object overhead for activating and deactivating parents of many objects. Any scripts with OnEnable and OnDisable functions would be called, and the fact that these functions can exist (whether they do or not) means that some logic must loop through every component and check whether they do. Other components must naturally perform logic at these same times (such as Renderer or Collider, for example) and must therefore have OnEnable and OnDisable (or something virtually identical).

More specifically, I suppose, there must be a per-component overhead for SetActive. So how bad it is depends on how complex your children are.

You are right. I thought i tested it at some point, but seemingly i’m imagining things since i did not use Unity back then.
Sorry for the confusion! I edited my above post and striked the misleading example.

1 Like

alright thanks everyone for the helpful discussion

When a GameObject gets deactivated through it’s parent being deactivated, OnDisable gets called on any behaviours attached to that object. So there’s for sure a cost.

It’s probably still worthwhile, unless your objects have an expensive OnDisable operation.

1 Like

So I wondered about this as well, because I set a couple thousand gameobjects to setActive(false) and setActive(true). Before I tested this was looping through a List of Gameobjects and turing each one off induvidually and it ended up taking about 16ms. But then I set the objects to a parent on Instantiation and turned that off and id did SO much better. It now only takes about 1ms so its defetnetly worth it to turn off the parent instead of every single Gameobject by yourself.

is this on editor or built? Because results will be different as editor tend to cache lot of stuff and behaves differently. If you want a proper, performance test, you should always try to do it in a build.