Search Unity

Dumb question about referencing prefabs

Discussion in 'Scripting' started by BigRedSwitch, Aug 30, 2011.

  1. BigRedSwitch

    BigRedSwitch

    Joined:
    Feb 11, 2009
    Posts:
    724
    Hey, all,

    I just wanted to check something so I know I'm not being an idiot.

    If I have an array, like so:

    Code (csharp):
    1.  
    2.      public GameObject[] Enemies;
    3.  
    which I then stock with prefabs from the library (which DON'T exist in the scene), are these objects loaded into memory, or just kept as references to the prefabs?

    In other words, if the prefabs I drag to the array in the editor are large objects with multiple materials, do they take up memory in my scene even if they're never used, or is the cost simply the size of an empty gameobject?

    Cheers,

    SB
     
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    If a prefab is referenced somewhere, then yes it's loaded into memory like any other object. Otherwise you wouldn't be able to clone it with Instantiate.

    If you really only want to load the prefab when you really need it, put it inside the Resources folder and use Resources.Load to get it the first time you need it.
     
  3. BigRedSwitch

    BigRedSwitch

    Joined:
    Feb 11, 2009
    Posts:
    724
    Nice one, Jasper. You may have just made my day. :)

    SB.
     
  4. BigRedSwitch

    BigRedSwitch

    Joined:
    Feb 11, 2009
    Posts:
    724
    Actually, another question regarding this:

    If you do have prefabs in the array, as mentioned above, as they're in memory (even if they're not in the scene), what is the state of the textures in their materials? I know that textures are decompressed before they're rendered, but as these objects aren't actually used in the scene, would their content still be compressed?
     
  5. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    I am not sure, but would assume that if more textures are needed they are loaded at the same time as everything else. They might not be pushed to the graphics card yet though.

    However, textures are only decompressed in memory if the graphics card doesn't support hardware decompression.