Search Unity

Reference to a prefab in the script will increase memory usage?

Discussion in 'iOS and tvOS' started by kevinjee, Jun 19, 2012.

  1. kevinjee

    kevinjee

    Joined:
    Nov 6, 2011
    Posts:
    1
    I declare some public variables in a script, and assign prefabs to them in the inspector. Like this:
    Code (csharp):
    1. public class EffectSpawner : MonoBehaviour {
    2.   public  GameObject fireEffect;
    3.   public  GameObject iceEffect;
    4. }
    Then I found my app's memory usage increase by 10mb. (iOS platform)
    When reference a prefab in the script, unity will load the prefab into memory even before instantiate it, right?
    If this is true, it will be so sad! Because I made lots of effects, which will be instantiated dynamically from code.
    If I can't assign them to variables in the script the only way i can do is put them in Resources folder and use Resources.Load
    I'm trying to reduce memory usage of my game, pls give me some advise.
     
  2. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    man i wish someone had answered this...
     
    sking_cg and MrEsquire like this.
  3. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    Just so this has an answer... If you have a public variable of any script 9n the scene, all objects use memory. So it's best to use Resources.Load to load them from resources folder instead of reverencing the prefabs in any script directly..
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,481
    Just so you are aware, there's a thread hinting that Resources.Load will be going at some point.
     
  5. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    Resources.Load will wait until you call it to load things into memory, but beware, on iOS this will often cause the entire game to pause for a split second the first time it is done as the asset is loaded.
     
unityunity