• 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
Question by Proclasteus · Sep 26, 2019 at 11:02 AM · scriptableobjectitems

Using scriptableobjects as consumable items?

Currently I'm using a scriptableobject class "Item" to allow me to easily make new items and modify attributes they may have. I also have an inventory that I can easily add items and everyt$$anonymous$$ng I have is working correctly. The problem I'm running into is that I want to override the Use() method so I can have different effects for each item. What is the best way to go about t$$anonymous$$s? Having to reference a gameobject with a script for a particular item doesn't seem desirable, since you would have to have a gameobject for each item, and my inventory is a list of Items and doesn't store gameobjects. Any advice would be appreciated.

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]

public class Item : ScriptableObject

{ new public string name = "New Item";

 public string description = "Item Description";
 public bool isDefaultItem = false;

 public virtual void Use()
 {
     //Use the item
     //Somet$$anonymous$$ng might happen

     Debug.Log("Using " + name);
     RemoveFromInventory();
 }

 public void RemoveFromInventory()
 {
     Inventory.instance.Remove(t$$anonymous$$s);
 }

}

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Mouton · Sep 26, 2019 at 10:35 PM

You could ac$$anonymous$$eve your goal with UnityEvents (https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html).


Add a UnityEvent effects field to your class Item , then in the inspector you can define the different effects you require.


 public class Item : ScriptableObject
 {
     public string name;
     public string description;
     public bool isDefaultItem;
     public UnityEvent effects;
          
     public virtual void Use(){
         if (effects != null)
             effects.Invoke();
     }
 }


To define your effects, you can store them in a single class ItemEffects.


 public static class ItemEffects
 {
     public static RestoreHealth()
     {
     }
 
     public static SetDebuff()
     {
     }
 }


You can also pass arguments to your effects with the generic version of Unity events (UnityEvent<T0>) and pass it a struct or a class.


 public class Item : ScriptableObject
 {
     public UnityEvent<MyParams> effects;
          
     public virtual void Use(MyParams params){
         if (effects != null)
             effects.Invoke(params);
     }
 }
Comment

People who like this

0 Show 4 · 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 Proclasteus · Sep 28, 2019 at 06:26 AM 0
Share

Thanks for the response! I was looking at UnityEvent and it seems that is derives from Monobehaviour. Wouldn't that make it incompatible with ScriptableObject? If it doesn't I guess I missed something.

avatar image Mouton · Sep 28, 2019 at 08:06 AM 0
Share

UnityEvents derives from UnityEventBase which is an abstract root class, it doesn't inherit MonoBehaviour.

It would not be a problem since effects is an attribute of the Item class, it will store reference to UnityEvents.

avatar image Proclasteus · Sep 28, 2019 at 07:31 PM 0
Share

I'm not sure why, but when I tried to implement it wasn't showing it in the inspector. But I just put what you suggested again and it worked! I'm not sure what I did wrong the last time. Thanks so much for the help, this feels like setting it up this way will make it scalable.

avatar image Mouton Proclasteus · Sep 28, 2019 at 09:40 PM 0
Share

Great ! Glad it could help you ! :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

185 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Make LoadObjectsOfTypeAll load objects that have not been loaded 0 Answers

How do I decide which Conversation to start when clicking on an NPC? 0 Answers

ScriptableObject not Serializing? 0 Answers

Can't you add tags using a script instead of tagging it through the editor ? 1 Answer

Keep data created at runtime in ScriptableObjects? 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