0
\$\begingroup\$

I have 2 questions about how i should approach this.

I want to make a 2D game, with alot of different creatures.

Question one: If i have alot of monsters and i'm using ScriptableObjects, since they have very similar variables. How can i make one Enemy prefab which will load the right ScriptableObject information? For example i have an Enemy Prefab, then it will load random monster between: Dragon, Frog, Wolf. How can i achive this without creating 3 new Prefabs for each one and instead load just the right ScriptableObject.

Question two: Should i create new prefab for each monster? Or is approach 1 the right way to do it, if you plan to expand over 100+ creatures.

Thanks

\$\endgroup\$
1
\$\begingroup\$

How can i make one Enemy prefab which will load the right ScriptableObject information?

I would put those scriptable objects on the script which spawns said enemies. Give it two inspector variables: Your general Enemy prefab and an array of your enemy ScriptableObjects. When it instantiates an enemy, it picks a scriptable object from that array at random and assigns it to the newly instantiated enemy. This allows you to set the pool of available enemies for each enemy spawner separately.

But if you want to avoid the hassle of assigning each new enemy type to the enemy spawner after you created it, then Addressables might be an option. The new Addressables system allows you to give each asset in your project an unique name. Scripts can then obtain that asset by using that name.

Should i create new prefab for each monster? Or is approach 1 the right way to do it, if you plan to expand over 100+ creatures.

Prefab variants could give you the best of both worlds. You can create a general Enemy prefab with all the components all the enemies have in common and with reasonable defaults for all settings on those components. And then you can create prefab variants for all the enemies which are based on that basic Enemy prefab. If you change something at the basic Enemy prefab, then that change will also affect all the variants which didn't override that particular setting.

This solution generates the same amount of clutter as the ScriptableObject solution. In both cases you will have one asset per monster in your project folder. But they give you a lot more flexibility because prefab variants allow you to override properties you didn't think about when you programmed your ScriptableObject. Like attaching new components or even new child game-objects to individual prefabs.

Oh, and you can even create prefab variants inheriting from prefabs which are variants themselves. This can be useful if you have families of monsters which share some properties but have a lot of properties in common (like palette swaps).

\$\endgroup\$
0
\$\begingroup\$

You need a monobehavior on the prefab that has your ScriptableObject as a variable Whenever you need to access any stats, you read them from the referenced scriptable object

Instantiate will return you a reference to the new GameObject, you can then use getComponent to access the Monobehavior on the newly spawned monster, and set the desired ScriptableObject to the variable.

You can either go on, and set up sprites and such in the same function, or use the start function of the monobehavior on the prefab to do the remaining set up, reading data from the scriptableObject.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.