Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 etaxi341 · Oct 15, 2019 at 07:54 AM · scriptableobjectinterfaceinterfaces

Interface on ScriptableObject

Hey! Is there any way I could add a variable to a ScriptableObject that accepts all classes that implement a specific interface?


Example:

  1. My Items are ScriptableObject

  2. Every Item should make something different for "void Use()"

  3. First I make an Interface "IDoInteractions"

  4. Then I make one Class for the Bow and one Class for the Sword that have the method "void Use()" from "IDoInteractions"

  5. I want to attach any of those classes to the corresponding ScriptableObject


Any chance to do this? Or how could I make my character do a different "void Use()" for every Item?

Thank you guys!

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

1 Reply

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

Answer by misher · Oct 15, 2019 at 08:16 AM

You can use tons of different approaches here, also using interfaces, the downside of interfaces are that you can't reference them in inspector later. Create a base class for your items, smt like this:

 public class BaseItem : ScriptableObject {
   public abstract void Use();
 }
 public class ItemSword : BaseItem {
   // implement Use here
 }

Try to experiment with different architecture and chose the best fit for you.

Comment
Add comment · Show 2 · 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 etaxi341 · Oct 15, 2019 at 08:25 AM 0
Share

Wow Nice idea! Got it to work like this! Awesome :)

I made it like this:

 public abstract class IItems : ScriptableObject
 {
     public string itemname;
     public int itemcost;
     public virtual void Attack1()
     {
         Debug.Log("The Item does not override Attack1");
     }
 }
 
 [CreateAsset$$anonymous$$enu(fileName = "Sword", menuName = "Items/Sword", order = 1)]
 public class Sword : IItems
 {
     public override void Attack1()
     {
         Debug.Log("I attacked with Sword");
     }
 }
 
 [CreateAsset$$anonymous$$enu(fileName = "Bow", menuName = "Items/Bow", order = 1)]
 public class Bow : IItems
 {
     public override void Attack1()
     {
         Debug.Log("I attacked with Bow");
     }
 }
avatar image dmpitu · Apr 12 at 03:39 AM 0
Share

Finally a solution! I have been trying changes without success. Thank you very much!

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

117 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

Related Questions

storing scripts that implements an interface 0 Answers

[C#] Calling Interface from Array of MonoBehaviour 2 Answers

IDropHandler only getting called when flicking the mouse 0 Answers

How to make a List that can store anything that implements an interface? 4 Answers

All interfaces in one Script 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