Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Ticool · Jun 29, 2020 at 04:08 AM · scriptableobjectenum

Scriptable Object Enum

Im trying to use Scriptable objects for dynmic enum ex : (Fire, Ice, Wind)

 [CreateAssetMenu(fileName = "New Attack Item", menuName = "Menu/Enum/Enum Attack Item")]
 [System.Serializable]
 public class EnumAttackItem : ScriptableObject { }

I got an other scriptable object that list those objects :

 public List<EnumAttackItem> EnumList;

How can i compare the scriptable object enum in code ???

Ex: I got a weapon with the attack type fire

How can i do :

 if (MyWeaponAttack == FIRE)
       // Code

Just like a real enum :

 public Enum(Fire, Ice, Wind)  AttackType
 
 if (MyWeaponAttack  == AttackType.Fire)

I dont want to hard code string ... i want the real type !

plz Help !!!

Comment
Add comment · Show 2
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 xxmariofer · Jun 29, 2020 at 06:40 AM 0
Share

what exactly is fire ice and wind? i mean, your scriptableobject is emtpy, fire FIRE is he name of the scriptableobject? simply add an enum inside the scriptableobject and compare that enum

avatar image Ticool · Jun 29, 2020 at 12:59 PM 0
Share

Fire ice and wind are the name of the scriptable object. If i add an enum inside my scriptable object it loses its ability to be dynamic from the designer if some one create a new scriptable object type... some one has to add it in code... Not shure whats the benefit to declare a enum inside scriptable object ??? You need to pass it to each gameobject after, just declare it in a class is better no ?

https://www.youtube.com/watch?v=6vmRwLYWNRo

Watch video at : 23:48

thx for help !

2 Replies

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

Answer by Bunny83 · Jun 29, 2020 at 11:32 PM

Well, you don't seem to get the point of using ScriptableObjects as enums. They are just empty objects and you can compare them. In order to compare them you have to have two references which you are going to compare. If you specifically know in code that you're going to have a "FIRE" value, you have to declare that in the script where you want to do the comparison and assign the FIRE asset to that variable. Something like that:

 // assign your FIRE asset here
 public EnumAttackItem FIRE;

 // now you can do that comparison you have in your question:
 if (MyWeaponAttack == FIRE)


However this does of course defies the point of having a "dynamic enum" value since your code demands that there is an instance FIRE. The main point of using ScriptableObjects as dynamic enum values is to completely decouple the code from the actual meaning of the value. The code shouldn't even know what values you may create in your project. Of course this depends on what you're actually trying to accomplish.


I highly recommend you watch the enum part of this brilliant Unite Talk (though if you have the time I can also recommend watching the whole talk).


The great thing about using ScriptableObjects as enum values is that you can add meta data or even functionality into the "enum value" itself. So you can tie in either behaviour or configuration data into the value itself so the code that is actually using your enum doesn't need to know what value we're talking about.


Your original code where you said if (MyWeaponAttack == FIRE) is just plain procedural thinking. If you want to use SOs like shown in the video you have to think more in OOP terms and take a more abstract approach to whatever you want to actually solve.

Comment
Add comment · 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
0

Answer by williamfalvo · Jun 29, 2020 at 02:33 PM

If you want only to do

if(MyWeaponAttack == FIRE)
//Code

you can set a name of object and do an if statement like this

if((MyWeaponAttack.name == "Fire")
//Code

I don't like much this solution but for your challenge it works

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 Ticool · Jun 29, 2020 at 11:16 PM 0
Share

Yea its already what im doing and i dont like it too.. i don't want to hard code string to compare them so like they say in the video im not shure its more dynamic than just code them in code im not understanding this approach... its simplify the designer job to add enum but for coder how we use those scriptable object in code. $$anonymous$$aking a dictionary of those enum and then make a foreach to parse all of them and compare sound like an idea but whats the gain ?

Refer to video :

https://www.youtube.com/watch?v=6vmRwLYWNRo Watch video at : 23:48

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

137 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 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

Allowing multiple selections on drop list via script inside inspector. 0 Answers

Character development with abilities 1 Answer

Using an enum inside of a scriptable Object - why don't I see it in the inspector? 1 Answer

Nested enum shows as int in Inspector 2 Answers

Changing Enum var order messes up Enums saved in Scriptable Objects. 2 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