Search Unity

Prefabs and ScriptableObjects

Discussion in 'Prefabs' started by ktest112233, Aug 7, 2019.

  1. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    Hi everyone,

    I have a ScriptableObject that holds references to prefabs placed in the folder Assets/MyCustomPrefabs

    Reference of this ScriptableObject is used by the LevelLoader Monobehavior script which uses it to get the prefab references and instantiate the object.

    My question.. is this the right approach to do so? And does this works fine in PC and mobile builds? Thank you
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Yes and no.

    It works, but it causes all of those prefabs to always be referenced. This means that they'll never be unloaded, so they'll be taking up space in memory even when not in use.

    Unless you're running out of memory, that probably won't be a problem.
     
  3. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    Memory is not going to be an issue so it would be totally fine having the prefabs in the memory.

    Is it also ok to load the prefabs from resources whenever needed and then just unload after instantiating?

    In my case it's only needed once when I am making the object pool and also when the pool runs out of objects
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That's also completely fine.
     
    ktest112233 likes this.
  5. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    thanks for the feedback. It cleared my mind about the approach to use