Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Memory consumption, prefab reference vs prefab instance

Discussion in 'Scripting' started by cesarpo, Aug 28, 2015.

  1. cesarpo

    cesarpo

    Joined:
    Jun 8, 2013
    Posts:
    97
    Do prefab references consume the same memory as prefab instances?

    What I mean by prefab reference: A public variable linked to a prefab not in the scene but in a particular folder.
    What I mean by prefab instance: The result of calling `Instantiate(prefabReference)`

    At first I thought that prefab references were just that, references, but later I realized that some of their functionality is being loaded too, except they haven't been "initialized" yet (Awake, Start, etc. weren't called yet), but things like `GetComponent` do work, which is weird to me.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    At runtime prefabs aren't anything different from the gameobjects that make them up. If you dropped it in the scene, it's an instance, not a prefab.

    The only difference is if you referenced a prefab by script to the prefab in one of your folders. When the game runs, an unactivated instance is created. Just like you noticed, Awake/Start hasn't been called. That's the only difference really.

    All scripts in the same scene referencing the same prefab will reference the same unactivated object in memory. So if you have 100 different objects referencing the same prefab, in the same scene, only 1 structure takes up memory. This isn't necessarily true when loading in asset packs and other scenes though (I'm not sure exactly how unity deals with them... they may or may not do some instance sharing...).

    They are 'references'. They're references to GameObjects and Components in memory.