Unity 2021 Use Scriptable Object instead of Enum

preview_player
Показать описание
"Why can't I create Enum values from all of my weapon in my prefab folder?!"
-me few years ago

When I was learning how to use Enums in Unity I knew that they are meant to represent state of something ex player has a WeaponType.Sword. This was wrong thinking. While they are good to represent ex cardinal directions (assuming that no one will add more) adding new enum for a new weapon type or anything that is certain to change / grow is not the way. Scriptable objects (and a bit of Object Oriented design) are. Let me show you why :)

Reference "Unite Austin 2017 - Game Architecture with Scriptable Objects":

Learn to write more maintainable code using Design patterns by making a 2d platformer game:

Project files for Patreons:

Have any questions? Join the discord:

Assets used:

#scriptableobject #unity3d #enum #csharp #sunnyvalleystudio #unitytips
Рекомендации по теме
Комментарии
Автор

I feel like not many people are using this approach (including me) so I thought is would be worth sharing again :)

SunnyValleyStudio
Автор

Actually one of the best scriptable objects tutorial for beginners, good job!

VladimirBerezkin
Автор

This is actually the first time I've understood ScriptableObjects and seen a usecase for them!

Mgtn
Автор

how to sumarize a 1 hour and 30 minute unity ted talk into 11 minutes :D, one of the best explanations I've seen so far.

juancamacho
Автор

i love this sort of stuff, not enough tutorials use real world examples to drive the point home

proportion
Автор

I'm serious, I went through 2 to 3 videos about this topic, but I still didn't grasp the complete understanding and its practical applications. This tutorial is the finest, tailored for beginners to comprehensively grasp what a scriptable object is and how to effectively implement them. Thank you for producing such top-notch content. Keep up the excellent work!

trexb
Автор

You have taken my coding to new heights with this tutorial. Thank you so much!

Luvseatshawty
Автор

This is by far the best Unity tutorials channel on Youtube
Lots of respect

timurradman
Автор

I keep trying my team to use more data oriented architectures to be more friendly for the designers, but one of the biggest reason is how you can easily add new content through CMS without having to update the game code, which can be amazing in the long run! Enums are great for some stuff, but content generation is not one of them!

joaniepepin
Автор

Simple, straight to the point ! I like it !

ArtRiku
Автор

Working right now. Cant wait to watch it later :D

mehmedcavas
Автор

A very nice tutorial, thank you a lot!

Dangerous_Milk
Автор

I find using enums mainly for "states" now, for example AI or a minigame states. When I started out coding I was exploiting enums and switch cases exactly as you demonstrated here. And as you mentioned it is scaling very badly.. Great video!

DavidZobristGames
Автор

Thank you very much! The greatest explanation i've seen.

ocbebbv
Автор

What do I reference in the enemy script If I want to deal damage to it? What do I put in the TakeDamage(WeaponManager and Weapon Scriptable Object here???)?

patek
Автор

This is very nice and simple example even for beginners. And case when you have enum and switch(es) is a warning you should do it differently. Otherwise maintainability will be hell.
Also good example with that traffic lights and compass, but usually it is not as clear as you work with game concepts.
I am currently struggling with situations, where I have several enums values, which areprobably not going to change much, and are connected to maybe one variable. So I dont feel it's rasonable to do SO just for one variable, but at the same time there are about 20 enum values, so I fear if I run to switch case with them.

BlackStarForge
Автор

This is where a good understanding of C#/OO would help any unity developer. What you are doing here is following the principles of good OO practice - encapsulation, abstraction etc. it’s nothing specific to unity.

PaulJonesy
Автор

Hello. What should I do if I have two identical bow prefabs in the scene that contain a different number of arrows?
That is, how to track a certain number of arrows for each individual prefab.

lifp
Автор

So, with these scriptable object weapons as an example, would you need an So for each weapon? For example, in most RPGs you've got multiple bow types, and multiple sword types, etc. Or would the so data container be able to hold the weapons class and, say, a range of damages and things that prefab objects could draw from? I might have missed this in the video (watched it twice) but I'm also completely exhausted at the moment and my brain is totally crashing at the moment ... Lol😅

onerimeuse
Автор

What i dont get is we could have used simple prefab instead scriptable object. What is the difference between scriptable object and prefabs in this particular scenerio?

EzioOmer
visit shbcf.ru