- Home /
Activating & Deactivating prefabs from a prefab pool VS simply moving them on and off-camera (iOS)
I use a prefab pool, instead of instantiating and destroying prefabs of collectibles that get spawned in my game and collected (hence removed from view). The game is for iOS.
The prefabs pool is a group of prefabs that are off-camera and deactivated when the level is loaded.
Whenever a prefab is supposed to get spawned somewhere on the board, it gets activated, moved instantly on board (and on-camera). When my character collects it, it gets instantly moved off-camera and deactivated.
My pool consists of 50 prefabs (that's the maximum potential number of prefabs that might be used simultaneously on-camera).
Is there any use in my having them initially deactivated and activating-deactivating them as described above? Would having them activated all the time and just moving them on and off camera make any difference?
Answer by Jessy · Jan 24, 2011 at 05:02 PM
Activating is not a fast operation; you don't want to do that. You could move them, or you could just disable their components. You could also add event listeners only at the appropriate times.
Thanks Jessy, but could you please be more specific? So moving the collectibles out of the viewable area when it has been collected, without deactivating it (using "gameObject.active = false;") is not using more resources (than if I had also deactivated it)? What component would it make sense deactivating (in order to spare resources I mean)? For example the collectible's "$$anonymous$$esh Renderer"?. I'll look into using event listeners as I don't yet know what they are.
Right, moving a game object is no big deal unless it has a static collider - use kinematic rigidbodies if you need to move things with colliders. Disabling renderers is the most obvious, but it depends on your game, what you need to do. Unfortunately, I don't know that there are any tutorials showing you how to integrates events into Unity; I never see anyone else using them, but of course most of the code on these community sites is just snippets, not reflecting an interwoven scripting system. Here is a small example, anyway: http://answers.unity3d.com/questions/34045