So I have recently come across an issue activating/deactivating game objects, its become to slow of an operation. More specifically, whenever one of my spawn triggers are executed the game pauses for a brief moment as the target gameobject is enabled. I have taken a better part of the day to try and find a work around to this issue, but haven’t had much luck.
I’m not doing any instantiation, this is a pure .SetActive(true) call on a game object and I’ve narrowed it down to being specifically that being the cause. My ideal solution would be to somehow toss the SetActive stuff onto a new thread, but I’m not even sure that’s possible, or how to do it if it was.
Curious if anyone has any good alternatives to SetActive()?
Have you deep profiled this? Calling SetActive should not be an expensive operation. More likely, something is happening in Start() that is expensive, and causing your aforementioned issue.
Of course, depending on what you actually want to disable, there are plenty of alternatives to turning off the gameObjects, such as disabling the individual components. What is it you are actually trying to accomplish, and what components are attached to the object? These are generally the causes of slow activation.
I am also encountering a weird variant of this.
When I initialise my scene, I am using GetComponentsInChildren to loop through and find all UGUI child game objects containing a particular component, so I can cache them in a dictionary for faster access later. The search starts at each canvas root parent in the scene, and all child objects are active. The weirdness comes in with the root parent being enabled or not.
Now, if the canvas object is enabled, no problem - it all goes nice and fast. But if I set the canvas disabled in the scene (which is preferable, to avoid flickering) then GetComponentsInChildren runs very slow and adds up to 5 seconds to startup. No problem, you say, just SetActive it before you start getting the components: but when I tried that, the slowness doesn’t go away even though effectively the objects are now in the exact same state as if I’d saved them as active in the scene. Weird.
Currently I am getting around it all by leaving the parents active but turning the camera off until I’ve done all the caching, to avoid the flickering. But I would much prefer to understand why this is happening like this, and why the mere presence of a disabled parent makes GetComponentsInChildren so considerably slow in the first place.