Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
4
Question by basemosen1 · Jan 23, 2018 at 03:18 PM · scriptableobjectenum

Extendable Enums with ScriptableObjects

I think that anyone who got interested in the usage of scriptable objects has seen Richard Fine's talk (link in case someone didn't : https://www.youtube.com/watch?v=6vmRwLYWNRo).
It's been really helpful and clear but the extendable enums things has not been explained with an example and i guess that it's so easy that it doesn't need one but for some reason i can't wrap my head around it.
Suppose i have this enum: enum DmgType {Fire, Water, Air}
if i have a field with a type of DmgType i can do the following : dmgType.Fire.
What interests me a lot with enum is that when I'm writing that i get all the possible types in my enum and If i'm assigning its value in the inspector i get a dropdown with all the possible values. So my question is how do you actually do that with scriptable objects

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Hellium · Jan 23, 2018 at 03:52 PM

You just have to create a simple ScriptableObject as follow:

 using UnityEngine;
 
 [CreateAssetMenu( fileName = "DamageType", menuName = "DamageType" )]
 public class DamageType : ScriptableObject{}

Then, in your other script, declare a serialized variable of DamageType type:

 using UnityEngine;
 
 public class Weapon : MonoBehaviour
 {
     [SerializeField]
     private DamageType damageType;
 }


You will be able to drag & drop a DamageType asset into the serialized field of your Weapon class.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image basemosen1 · Jan 23, 2018 at 04:13 PM 2
Share

Thank you for your quick answer, basically i already knew this part but is it really all that it's about ? just creating multiple DamageType assets then dragging the one you want to assign to your weapon ? I mean as i already mentioned in the question, one of the things that interest me the most is having the ability to get what damage types do i actually have. How will i test if this weapon's damage type is Fire or Water ? can this usage of scriptable objects help with having a drop down in the inspector containing all the DamageTypes i have already created.

avatar image Hellium basemosen1 · Jan 23, 2018 at 04:16 PM 1
Share

In the inspector, once you have the serialized DamageType field, click on the little circle with a point inside, and the assets explorer will pop-up. You will be able to select the desired scriptableobject from there. It's a little bit like the drop down of the enumerations.

avatar image baptanglet · Nov 17, 2019 at 01:22 PM 0
Share

I am a bit late on this post but I am exactly looking for the same thing : having a dropdown by code of all SO enums added in inspector like "damageTypes.Fire || damageTypes.Water" where Fire and Water are the names of the SO serialized in inspector

@Hellium, thank you for trying to help but you didn't answer the question

avatar image
3

Answer by OAlexM · Jan 10, 2020 at 08:17 AM

I know this post is 2 years old. But it is on top of the google search. So I try to give an answer to this. The problem with enum as a code construct is, that you have to check what kind of damage the weapon is doing before switch / case to the correct value of damage amount for example, or to which effect you play if the weapon hits. It would look like:

 val damageAmount = 0;
 switch(damageType)
 {
 case DmgType.Water:
     damageAmount = 2;
     break;
 case DmgType.Fire:
     damageAmount = 4;
     break;
 // ...
 }

The same for all other decisions in code where you need to know what data you need for the current DmgType. Assume you could ask the enum itself for its damageAmount. Like this:

 var damageAmount = damageType.amountOfDamage;

It is a one liner. The thing is you could put all these configuration values inside the ScriptableObject class you implemented for your enum type. For every other needed configuration do the same provide a property field in the ScriptableEnum class, that's it. Even conditional questions in code could be solved very easy. For example if you have to decide if the weapon is usable on the enemy. For example there is a fire demon. And your weapon has the DmgType.Fire it is useless against this fire demon. Normally you make an if in the code. But think about the if then elses you need per enemy type to decide if the weapon is usable against them? With the scriptable enum it is easy just implement a:

     List<EnemyType> beatable;
    bool CanBeat(EnemyType enemytype)
    {
         return beatable.Contains(enemyType);
    }

into the ScriptableEnum class where you can easily put in all types of enemies that are beatable. And the best thing is, if you have this kind of ScriptableEnums all over your code, it is absolutely easy to implement a new weapon without changing one line of code. Just create the new asset object and define all values, conditions and prefabs there. It will work all over your code automatically. The funny thing is you will never have the need again to know if the weapon has the DmgType of fire. :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eristen · Mar 05, 2021 at 10:20 PM 0
Share

This answer helped me see the benefits of using scriptable object "enums" (i find it better not to think of them as enums because enums are just an array of strings really, while these extendable objects are a beast. I used them for creating different game phases, so for example instead of having an enum that looked like this:

 public enum GamePhase{
   DayPhase.
   NightPhase.
   HellPhase
 }
 

and comparing values in controllers with values in the enum now I have separate scriptable objects of class GamePhase that have all the info a gamePhase has, like duration, skybox, and whatnot, so in a sense I moved all the data from the controllers to the Scrptable Objects. Awesome stuff

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to use enum value to reference a variable in scriptable object? 0 Answers

How to redraw a texture on a prefab instance when a property in the inspector is changed by the developer? 0 Answers

Why is Select image Dialog empty when trying to populate ScriptableObject? 0 Answers

Scriptable Objects inside other SO's not loading properly from Asset Bundle 1 Answer

Storing levels data as json? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges