- Home /
Interchangeable Objects/Prefabs
when needing things to be created into the game world I understand there are 2 options:
instantiate
, such as firing a gun to instantiate a bullet to destroy said bullet on impact.I know that one alternative to instantiating (as it can lead to memory fragmentation, and on mobile platforms is almost a bad joke) every time an object is to be created in the world is to find the maximum number of those things that "could" exist in your game, and then when they are needed get one that is not being used then reposition, reorient, and render it.
but at least in my mind this second approach only lends itself to be used with the same things.
for example: lets I have 3 types of bullets. the difference between the bullets will be that they each have a different child class of an abstract base class, and a different model. now all of these bullets have an equal likelihood to be created in the game, and therefore have an equal likelihood to be needed. I would like to use option2 above, but I would like to do it where I can interchange the model, and scripts. almost like interchanging the prefabs themselves.
Is this possible? How?
bullets were more an example of this. I was wanting to be able to do this with bullets, enemies, and so on.
essentially be able to interchange any Object of TypeX with similar. like I have all these enemies in the level ins$$anonymous$$d of destroying them ins$$anonymous$$d I just stop rendering them, and then replace them with a different type of enemy later as needed.
similar to treating the Objects/prefabs themselves as components/templates.
Answer by AndyMartin458 · Jan 05, 2013 at 09:22 PM
For the scripts specifically, by using inheritance you can maintain an array that is the maximum size which holds base class scripts. This array can hold any of the 3 child scripts as well. Perhaps this would also be the way to do what you're talking about. Create an array to hold your max amount of game objects.
From my understanding of Unity, you can't really do this with prefabs directly. An easier option (though costly in performance) would be to create the maximum amount of all 3 kinds of objects. Then no matter what, you would be able to have that many objects on the screen. However, I don't think that a game studio would follow that methodology for bullets. I think that they instantiate a bullet when it is needed and delete it when it is no longer necessary. What is the lifetime of the bullets?
I mean, it sounds like an interesting idea, but I don't think that's how Unity works. It kind of violates the typical best practices.
If you look here http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html under "Replacing a character with a ragdoll or wreck", they essentially say that it's much better to delete the old one and instantiate a new prefab ins$$anonymous$$d of trying to have fancy code on the original prefab.
Say you have 3 enemy types, and you want to populate a level with 10 random enemies. Then, you create a dictionary that holds a reference to the 3 types of enemies. In Start, use the random function to instantiate an enemy and add it to an enemy array (maintain all your enemies in an array of game objects, a dictionary of the three different kinds, or however you see fit). Then you can still have a bunch of different enemies up to your maximum number (or even more), but you won't have to deal with dynamically changing your prefab.