Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Assigning a prefab to public GameObject variable through inspector vs Resource.Load

Discussion in 'Scripting' started by GR, Jul 3, 2014.

  1. GR

    GR

    Joined:
    Aug 13, 2013
    Posts:
    2
    Hello, I have a cannon in the scene, and it has a script that has:

    Code (CSharp):
    1. public GameObject[] allProjectiles;
    2. public Transform projectileSpawn;
    3. private GameObject equippedProjectile;
    4.  
    5. void ChangeProjectile()
    6. {
    7.      int projId = Random(0, allProjectiles.Lenght);
    8.      equippedProjectile = allProjectiles[projId];
    9. }
    10.  
    11. void Update()
    12. {
    13.        if (Input.GetKeyDown (KeyCode.Space))
    14.        {
    15.             Instantiate(equippedProjectile, projectileSpawn.position, projectileSpawn.rotation);
    16.        }
    17.  
    18.        if (Input.GetKeyDown (KeyCode.Q))
    19.        {
    20.             ChangeProjectile();
    21.        }
    22. }
    to witch I assigned all prefabs from the ProjectView to allProjectiles.
    My questions are:
    -What if I have 100 different canon ball prefabs and I assign them to my cannon script through the inspector will that take up ram memory? I'm asking cause I think a prefab is a container that holds data on HD, not an actual instantiated GameObject. And If I simply assign a prefab from ProjectView to a script variable of type GameObject, an instance instance must be created from that prefab somewhere in the memory to witch ie. a allProjectiles[0] holds a reference.
    -If the answer to my previous question is "Yes", does the next code modification solve my problem?

    Code (CSharp):
    1. public string[] allProjectileNames;
    2. public Transform projectileSpawn;
    3. private GameObject equippedProjectile;
    4.  
    5. void ChangeProjectile()
    6. {
    7.      int projId = Random(0, allProjectileNames.Lenght);
    8.      equippedProjectile = Resources.Load(allProjectileNames[projId], typeof(GameObject)) as GameObject;
    9. }
    10.  
    11. void Update()
    12. {
    13.        if (Input.GetKeyDown (KeyCode.Space))
    14.        {
    15.             Instantiate(equippedProjectile, projectileSpawn.position, projectileSpawn.rotation);
    16.        }
    17.  
    18.        if (Input.GetKeyDown (KeyCode.Q))
    19.        {
    20.             ChangeProjectile();
    21.        }
    22. }
     
    Last edited: Jul 3, 2014
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Is having 100 prefabs in memory a problem? Also - Resources.Load stores the object in memory in order to prevent duplicate loading.
     
  3. GR

    GR

    Joined:
    Aug 13, 2013
    Posts:
    2
    Well I'm developing for mobile and I was instructed to keep the memory as free as possible. And btw will my problem be solved if I then use Resouce.UnloadAsset() to free the memory from the current GameObject, and then I use Resource.Load(), to set the reference to new loaded GameObject?
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    If you actually use those 100 different object, they will take that much space in memory. There's nothing you can do about it. The only thing you can do is it you are actually not using them.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'd be more concerned about the performance of calling load/unload a bunch of times before I was concerned about how much memory I was using.