'Performance of calling gameObject.SetActive(true) when game object is already active?

Is there any performance concern around calling MyAlreadyActiveGameObject.SetActive(true) a ton, e.g., once per frame?

Put another way, is it ever worth pulling a gameObject.active check upwards? Or caching/checking an _alreadyActive?



Solution 1:[1]

When in doubt, try the simple thing first and see if it works. Sometimes it's worth focusing on optimization, but you should check if it's necessary effort. Unity's built-in profiling tools can give you an estimate on how long each operation is taking in each frame.

Is there any performance concern around calling MyAlreadyActiveGameObject.SetActive(true) a ton, e.g., once per frame?

It does take time, but not much. If you're doing this hundreds or thousands of times per frame, it might be worth worrying about. Up until then, the concern should be negligible.

If you are worried about performance for a large number of objects, you can look into pausing expensive behaviors for objects which are far away, out of view, etc.

Solution 2:[2]

If you are concerned about this, you could always do a basic if-check on the same line.

if(!myObject.activeSelf) myObject.SetActive(true);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rutter
Solution 2 obieFM