0
\$\begingroup\$

If I create a prefab out of an object on scene will my game take more memory in Unity?

I have 30 enemies on my scene. I created a prefab for every one of them. I am going to use each of the created prefabs to respawn enemies in the same places where I put each one of them, with the same configurations.

Now, my question is if it is ok to create a lot of prefabs? Will Unity know how to prevent them taking a lot of space? Or will I have troubles with memory when I will create more levels (read here scenes) with even greater amount of enemies, given that each enemy will have its own prefab?

\$\endgroup\$
1
  • \$\begingroup\$ This looks like a question you can answer for yourself by generating a build without prefabs, then generating a build with prefabs, and comparing their memory use. What have the results of this experiment been when you've tried it? Have you measured any meaningful difference at all? \$\endgroup\$
    – DMGregory
    Jun 16, 2021 at 22:22

2 Answers 2

2
\$\begingroup\$

.PREFAB files are just text files taking very little space. Prefabs are just saved information of how the game object should be configured or what components should be attached at the time of instantiation into the scene. So, creating lots of prefabs can consume very little memory or disk space only in the editor mode. But in the final build, there is no effect of creating lots of prefabs.

\$\endgroup\$
2
\$\begingroup\$

It depends on how you use them, but generally prefabs will take memory in the game (not just in the editor), even if you do not instantiate them.

More concretely, any prefab that is referenced by a loaded scene will be loaded to memory as well. You can avoid this by not referencing your prefab directly, and instead put it in the Resources folder and instantiating it by calling Resources.Load. This way, the memory will be used only when you need the prefab, and not when the scene loads, which might have a huge impact if you use lots of prefabs (or large prefabs).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'd recommend Addressables over Resources.Load. They allow more granular loading and flexible packing of dynamically-loaded assets into asset bundles and the like. \$\endgroup\$
    – DMGregory
    Oct 24, 2022 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .