Using ScriptableObject in Unity

Prasetio Nugroho
2 min readApr 5, 2020

What is ScriptableObject?

A ScriptableObject is a data container that you can use to save large amounts of data, independent of class instances — unity manual

In any case, ScriptableObject is useful for sharing data as data container between scene. in Example you have string and float value in scene A and then you want to use it in scene B. Just use script with ScriptableObject value to load the data.

Another example, sometimes you need a value from your scene script into instantiate prefab. You can use a ScriptableObject to store the data and then access it by reference from all of the Prefabs. It makes more modular.

ScriptableObject as Data Container

In this case i want to load float and string from scene A into scene B. So i make a script with CreateAssetMenu attribute to make a ScriptableObject.

To create the ScriptableObject, right click -> create -> ScriptableObjectDataContainer -> Item Data

creating ScriptableObject

The ScriptableObject will be created. Then you can fill the data.

ScriptableObject
ScriptableObject data

After that i make a script to load the data from ScriptableObject and change the scene.

Add the script into empty game object or whatever game object you want. Because i want to use the data in Scene A and Scene B, so I use the script in Scene A and Scene B. Don’t forget to add the ItemData reference ScriptableObject.

Loader script in scene

Play the scene! I use Q button in keyboard to change into SceneA and W button into SceneB. Check in console, there will be some log that the data from ScriptableObject is loaded.

Result

If you want ScriptableObject way in advance , you can read from unity article about ScriptableObject. There are many ways to architecture your game. Hope you enjoy it!!

--

--

Prasetio Nugroho

Interested in new tech, gaming, programming and another knowledge or possibilites that can make me better. Cheers!!