0

When we still make multiple copies/Instances of ScriptableObject then how is it good for memory optimization.?

In Past, scripts were attached to prefabs and on instantiation, every script was used to create/reserve its own slot in memory, which was a bad approach because there were multiple copies of the same script.

What we do in scriptable objects is: create a scriptable object, create its multiple instances from Asset>Create> , feed data in those instances and access data when required. How it's is different from attaching script on prefab and instantiating it OR making multiple copies of ScriptableObject having data in them and access data from it.? After all we are making multiple copies in the scriptableobject as well.

Kindly explain the difference that how multiple copies of ScriptableObject are memory friendly then multiple copies of prefabs that are instantiated.?

  • I thoght that scpritbleObject have no MonoBehavior cost. it more simply , and treat it as a setting file is good enough. – TimChang Nov 11 '19 at 10:16
  • @TimChang I think we don't inherit it from monobehaviour just to tell unity that this script cannot be attached to any gameobject, but it still uses monobehavior functions. – Umer Farooq Nov 11 '19 at 10:26
  • 1
    Prefabs and SO have different purposes, prefabs will likely use SO as provider of data (sprite, other prefabs, data). You can have multiple prefabs using the same SO (this is where the memory consumption comes in). SO are not MonoBehaviour, they don't have their lifecycle (Start, Update,...) they are mostly about storing data or events. You could think of them as an advanced C# class that you can use them in Editor. – Everts Nov 11 '19 at 12:23
  • If you create all your SO on runtime then you probably should rather use a "normal" class and create instances on runtime. Otherwise as Everts already stated: The purpose is completely different. SO can be seen as serialized DataContainer so you can store information in them and reference and especially also exchange them where needed. Prefabs are templates for GameObjects or entire parts of a hierarchy you want to Instantiate into the scene. Unity unfortunately has some confusing names like `OnEnable` `Awake` for SO but they behave different to `MonoBehaviour`! – derHugo Nov 12 '19 at 06:06
  • Thank You all, everything clear now :) – Umer Farooq Nov 16 '19 at 19:58

1 Answers1

1

ScriptableObjects are much more lightweight as compared to MonoBehaviours as they don't have all of the GameObject data to deal with. Yes, they still have some Unity.Object overheads as compared to a System.Object, and there's a couple of life-cycle hooks available, but they also don't get put into things like the Update queue.

You should think of ScriptableObjects as pure-data objects (eg, settings/configs) for your designers to edit in Unity.

Immersive
  • 1,684
  • 1
  • 11
  • 9