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 /
avatar image
0
Question by digiholic · Jan 14, 2018 at 04:16 AM · scriptableobjectmonobehaviourbehavior

Adding a behavior to a ScriptableObject

So, here is the situation I'm in. I have different small parts, similar to cards in a TCG, that have different stats and effects. This is an idea situation for a ScriptableObject, since I can create an asset for the cards and enter in the data for things like health, name, etc., but not for effects. I can't have a single ScriptableObject for everything since I need them to have different methods for various triggers.

There's a few ways I can think of to do this, but none of them seem particularly ideal. One is to have reference to a MonoBehavior, and having the "card" objects hold a reference to both a ScriptableObject and a MonoBehavior, but that almost entirely defeats the purpose of using a ScriptableObject, since I might as well just hard-code the data into that MonoBehavior.

Another way is to have every single possible effect in the ScriptableObject .cs file, and have an event selector or message for the ScriptableObject to know what method to call when its triggers are met. This is incredibly messy and difficult to maintain.

Is there any other way to add modifiable methods to a ScriptableObject? For example, having a method named "OnAttack" for two different cards, and set one to a method that deals damage, and another to one that will instead heal itself?

I would like to keep using ScriptableObjects if possible, as they are much quicker to create and edit, and I don't have to worry about updating many instances and prefabs if I need to change a specific card's stats.

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 timmccune · Mar 24, 2019 at 07:53 PM 0
Share

I am trying to do a similar thing. I am working on an RPG that has a number of Races(That may have some skills) and a number of Professions(That may have some skills). I created a SO called Race and then created a few Races from the assets menu(Setting their individual stats). I did the same for the professions. I had hoped that adding the skills to each Race/Class would be as easy as adding the following to both Race and Profession:
public ScriptableObject[] skills;

then writing some skills(hide, sneak, cast, s$$anonymous$$l, etc) that derive from ScriptableObject, and attaching the appropriate ones to each Race and profession. But apparently, that isn't an option. I've been digging through google searches to find the right solution but I feel I'm at a dead end. Any suggestions would be greatly appreciated.

avatar image Bonfire-Boy · Mar 24, 2019 at 08:09 PM 1
Share

I may be missing something but isn't this just asking for polymorphism?

 abstract public class Card: ScriptableObject
 {
     abstract public void OnAttack(...);
 }
 
 public class DamagerCard: Card
 {
     override public void OnAttack(...)
     {
     }
 }
 
 public class HealerCard: Card
 {
     override public void OnAttack(...)
     {
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

76 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

Related Questions

MonoBehavior, ScriptableObject and Object memory usage & performance 0 Answers

Are MonoBehaviours heavier than ScriptableObjects? what's the difference in memory footprint between them? 0 Answers

Adding inherited class to list 1 Answer

Best practice for many similar scripts in a game? 0 Answers

AudioClip in ScriptableObject 1 Answer


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