Scriptable Objects.

Suleiman Abdullah
4 min readNov 4, 2024

--

Objective: Understand Scriptable Objects.

Scriptable Objects , this the data containers which is Independent of class and it allow use to avoid duplicate of data at runtime and save memory usage when storing data in editor session and runtime.

Note: Scriptable Objects are unity special type so you wont able to create them outside unity.

How to Use scriptable Objects.

We need to create a script which Inherit scriptable Object class.

Important!

When class Inherit scriptable Object can not be attached to game Object.

Creating scriptable Object.

To create scriptable Object there are two option but since am using Old unity version I will create scriptable object using CreateAssetMenu.

Using CreateAssetMenu attribute go to the script which inherit Scriptable Object. Then open square bracket then type CreateAssetMenu inside, then open double parenthesis.

Note: The tool tip will show some attribute needed, don't worry am going to explain what goes in there in next paragraph.

When typing [CreateAssetMenu()], Inside double parenthesis the first need to be fileName which will popup automatic if your try to type that name. Then you need assign that file name with equal sign with a name you want but the name needs to be inside double quote.

If we try to create scriptable Object like this will be created but its not Ideal.

We need to add other attribute, to do this we need to add comma then type this words menuName followed by equal sign, then add double quote, inside double quote we will type Scriptable Object with forward slash then type the type of file want to create.

Note: This menuName is popup window will appear when we create our scriptable Objects to give us insight what kind of scriptable Object we are creating.The fille name is the one with attibute fileName

Now Lets create the scriptable Object

Adding variable or Method in scriptable Object.

Accessing scriptable Object.

Create new script called Test attach it to the game Object, then create a variable of type ScriptableObjectTamplate.

Calling method using scriptable Object method from other script.

Now select the object which has test script and drag and drop scriptable Object asset to the inspector. Then play you will see our method from scriptable object is run without any problem.

Bonus

Creating Enemy wave using scriptable Object.

First we create our scriptable Object with a list of Game Object.

We create two Scriptable Object container and specify how many enemy each of them will store.

We create a Spawn Manager which is Inherit MonoBehaviour, and attached to the gameObject.

Inside Spawn Manager we have a list of scriptable Object and int variable of wave count then drag and drop of the scriptable Object from asset to the Spawn Manager into the inspector.

We have a Coroutine which run a loop, then store the list of gameObject through scriptable Object list.

We loop through all currentWave which is the list of enemy inside the first Scriptable Object. We instantiate each enemy and wait for 1 second.

We wait for five second then destroy all the enemy how I have create local game Object and make every enemy Instantiated a parent of this game object(“Previous Wave”) so if this object is destroyed everything destroyed inside it, in this scenario it depend on how you want your enemy to despair this is a quick example.

If the first wave destroyed we Increment waveCount which goes inside the index number of scriptable object list (“enemyWaves”)

Then we check if waveCount is equalt to enemyWave dot count, we break the loop , if its not true we go up the to Instantiate second wave.

See you into the next article.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet