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 Rukas90 · Sep 29, 2018 at 10:26 PM · inventoryinventory systemitemslotuse

Best approach in making Inventory UseItem functionality.

Hello everyone,

I've been trying things out lately and ran into one 'issue'. I made an inventory system. Everytime you right click with mouse on the inventory slot that contains an item you get a menu with two options (Use, Drop).

Drop method is simple, but I'm not sure with Use, because I might be selecting any type of item (e.g. Weapon, Health kit, keycard) and these all items have different use methods (e.g. for Weapon I would like to equip the selected weapon, for health item I would like to heal, for keycard use it on a door if able).

Whenever I select the item and display the menu I send the Item scriptable object with all the item data. So it's no telling what item it is. Well I could create an enum and specify the type and then do a switch and according to the type of item do something. But that could become really messy real quick.

What would be the decent / better approach in making this (Use functionality)?

Thank you!

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
0

Answer by Pakillottk · Sep 30, 2018 at 12:24 AM

Hi, the usual approach to solve this problem it's inheritance. You derive your base Item class which have a virtual method (i.e. void Use()) and override It to achieve the desired effect. Then when in your UI Use is requested you call Use() and the correct method it's executed.

What I've been doing recently it's making and abstract ScriptableObject called Interaction with an abstract method called Interact. Then I make all my Interactions as children (Start Dialogue, Add item to inventory...) and set it all up from within Scriptable Object turned into assets. Then my Items have an Interaction field called OnUse and I attach the desired Interaction to take place.

Hope that helps!

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 Rukas90 · Sep 30, 2018 at 01:45 PM 0
Share

Hey, thanks for the suggestion! Your suggestion seems interesting, tho what I thought about using is Events more specifically UnityEvents. So what I actually ended up making is adding UnityEvent to the Item class. Then inside a curtain class (e.g. WeaponSwitch) I created an array of Items, where I assigned all the Weapon Items and OnEnable added Equip method Listener to the Use UnityEvent. And OnDisable removed all listeners from the Use UnityEvent of course. When I open the menu I grab a reference of the selected item and whenever I press 'Use' button I just simply access item's Use UnityEvent and simple invoke it and that Invokes all added listeners. I don't know if this is the best way in making something like this, but it works quite well tbh!

avatar image Pakillottk Rukas90 · Sep 30, 2018 at 02:37 PM 1
Share

That's an interesting solution. The only thing I'd look into it's switching from UnityEvent to native c# events, because these are more perfomant and given that you are doing everything by code I think that you can afford losing the convenient Inspector editor form UnityEvents.

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

92 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

Related Questions

Deleting item from inventory system... 1 Answer

How can I transfer the item's image in inventory UI to the next scene?,How can I keep and transfer the image of items in the inventory in different scenes? 0 Answers

Grid Inventory : Move Item & Slot Highlighting 2 Answers

Invoking Effects of (Random) Items (C#) 0 Answers

[C#]Inventory script help. 3 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