I am making a dungeon crawler where each room can spawn its own enemies. Right now I need a way to reference the enemy prefabs. One way of doing it is by having the references on the room script, like this:

public class Room : MonoBehaviour
     [SerializeField] Enemy enemy1;
     [SerializeField] Enemy enemy2;
     [SerializeField] Enemy enemy3;
     // etc..

Another way of doing it is by having a static AssetHolder class that has all references that can be queried like so:


I honestly prefer the first one in terms of code (one less class, better structured) but I want to ask which one is better in terms of memory usage and performance.


  • 1
    \$\begingroup\$ I don't think you'll find that this choice has a substantial impact on memory usage or performance. You might spend a few more bytes one way and take an extra indirection the other - so unless you're storing/accessing tens of thousands of prefabs this way in a frame, it's unlikely to cause any issue. Profile your favourite way of writing this, and measure whether it causes a problem that matters for your game's use case. (However, anytime you start naming your variables 1, 2, etc, it's a good sign you should probably be using an array instead) \$\endgroup\$
    – DMGregory
    Aug 20 '21 at 2:15

You must log in to answer this question.

Browse other questions tagged .