I’ve tried searching for this, but I’m not actually sure what search terms to use. Also it occurs to me that this may not really fit in the Scripting forum, but I’m not sure where else to put it.
Say I’m making a mining game with several different “minerals”, each with the same C# script attached which holds variables like value, current “health” etc. I’m obivously going to have multiple clones of each different mineral type during the game. Is it better to have one single mineral prefab, and then assign different values and textures to that prefab through a function after instantiating the object, or is it better to make a different prefab for each mineral type so the textures and values are set at design time? I can do it either way, but I do not have the necessary knowledge to determine what performance costs are associated with each method.
As for performance - if an object is not instantiated - there is no cost associated with it - since it does not yet exist in the scene. So prefab or not - performance will be dictated by “how much” you are putting on-screen, prefab or otherwise. That would be my “guess” since I haven’t tested.
Some of the best performance increases you can do is not to instantiate stuff until the player is about to see them - so off-screen, if possible, or have them set to go in the scene and use a culling mask to keep them from line of sight until you are X distance from the object.
In a 2D world - anything off-screen can be hidden with a mask or not instantiated until you are off-edge of the screen with a camera. 3D it all depends on how soon you want that object to “pop in” a person’s view - which can be handled by a new instantiation or mask.
OK, so no performance issues related to either method. Is there any reason to use one method over the other? Or is it basically just my preference over whether to use one prefab for all minerals, or a seperate prefab for each type?
Setting variables after will give more flexibility by adjusting adding a few lines of code for an all-new stat-type - attaching them to a prefab would mean you’d need to make each prefab individually and edit each individual prefab if you want to make changes.