- Home /
How to initilize game objects on runtime and manage them?
I am working on a multiplayer game where objects must appear on demand (whenever a player request an object or make the objects appear randomly in random places) this means that there will be so many objects, but In addition to initialize them on run-time i also need to manage them, obviously i can't have that many objects without affecting APK size and performance, so i need some why to reference objects in a way that does not affect performance.
As an example to my case, take Pokemon go for instance, where objects appear randomly in random places and there are so many of these objects (maybe millions) across the map, how is that done?
Thank you
Answer by jacobschellenberg · Jun 26, 2017 at 05:36 PM
Prefabs. -> https://docs.unity3d.com/Manual/Prefabs.html
Prefabs allow you to create essentially a template, in which you can assign to a script to be instantiated at a later time with GameObject.Instantiate(prefab, position, rotation). See: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
So for the Pokemon example, you'd have a prefab for each Pokemon, and instantiate(spawn) them into the game scene as needed. And when you're all done with them, Destroy them, or put them back into a pool to be recycled and reused later.
Prefabs are your friend.
thank you, that make sense, but is prefabs actually of type Transform? (i've seen it on this line of code : public Transform prefab;
)?