- Home /
Performance: Multiple asset references vs. One public asset reference
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
{
[Header("Enemies")]
[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:
AssetHolder.Enemy1Prefab
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.
Thanks.
Answer by AaronBacon · Aug 20, 2021 at 10:40 AM
Technically, static variables are more memory heavy than local variables, but in this use case, with just a few enemies, it really won't make any difference. Generally, the main things that affect performance are spawning or destroying large amounts of objects, as well as obviously having a lot of unoptimized objects in camera view, like high poly models.
Answer by joan_stark · Aug 24, 2021 at 07:21 PM
If what you are looking for is a way to reference different type of enemies, I would create a scriptable object that have references to all enemy types, and then you create a variable that holds this scriptable object. Scriptable object will only create a unique instance across all rooms so it will be performance friendly, and also it will be a clean way to look for prefabs and no static classes.
Also, 100% sure you will need a Singleton class that controlls some logic of the game, so maybe you can reference the scriptable object there, and access it fastly with the singleton class.
Your answer
Follow this Question
Related Questions
ManagedHeap.ReservedUnusedSize 0 Answers
Why does Convert.ToDecimal use Double.ToString internally? 1 Answer
UI.Text: Lerping scores in Update -- excessive String allocation? 0 Answers
Do non-rendered gameobjects use memory? 1 Answer
Can low memory in the target device (Oculus GO) affect my game performance? 1 Answer