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 tuncOfGrayLake · Jun 29, 2017 at 04:29 PM · inspectorinventoryinheritanceitem

Inventory Management Issue

I have a GameObject you can walk on to pick it up. This GameObject has an ItemContainer class extending from MonoBehviour. ItemContainer has an Item property, which does NOT extend from MonoBehaviour.

When I pick this GameObject up I look within the ItemContainer and retrieve the Item within this class. I add this Item by copying it to my Inventory which has a list of items. Once I'm done, I delete this GameObject as we've picked it up already.

As expected I have various items I'd like to see in this game so when I make a Medkit class extending the Item things become problematic.

The Medkit class can override methods like DoOnUse which should apply healing effects to the player, however, my ItemContainer doesn't allow me to specify the type of Item in the Inspector. Eventually I can't configure my Items to be a child of Item in the ItemContainer's inspector.

If I got rid of the ItemContainer.cs and converted the Item class to extend from MonoBehaviour then adding the items to my inventory necessitates collecting them as GameObjects which is problematic if you have one hundred items of different types in your inventory.

If I used a Container for every child I have then that's too many scripts you have to do duplicates for and feels like a dirty solution.

If I wrote an Editor class extending ItemContainer's inspector then it's a lot of effort for something so simple and again there are so many steps just to make a simple Item.

I humbly ask for your help.

ItemContainer.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ItemContainer : MonoBehaviour
 {
     public Item item;
 
     public void Awake()
     {
         SetGameObjectOfItem();
     }
 
     public virtual void SetGameObjectOfItem()
     {
         item.myGameObject = gameObject;
     }
 }

Item.cs

 using UnityEngine;
 
 [System.Serializable]
 public class Item
 {
     [SerializeField]
     public GameObject myGameObject;
 
     [SerializeField]
     public string uniqueID;
 
     [SerializeField]
     public string name;
 
     [SerializeField]
     public string pickUpText;
 
     [SerializeField]
     public bool isQuantifiable;
 
     [SerializeField]
     public int quantity;
     public void IncrementQuantity()
     {
         quantity++;
     }
     public void DecrementQuantity()
     {
         quantity--;
     }
 
     [SerializeField]
     public bool isEquippable;
     public virtual void DoOnEquip(){}
     public virtual void DoOnUnEquip(){}

     public virtual void DoOnPickUp(){}
   
     [SerializeField]
     public bool isUsable;
     public virtual void DoOnUse(){}
 }

Medkit.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Medkit : Item
 {
     public override void DoOnUse()
     {
         base.DoOnUse();
         //TODO: Insert healing code.
         Debug.Log("Healing player!");
     }
 }
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

2 Replies

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

Answer by tuncOfGrayLake · Jun 30, 2017 at 01:22 PM

Since I wouldn't be having a great deal of variety of items in this game and the impact of performance wasn't as strong due to this fact I've made a decision bearing all this in mind. I modified my scripts to extend from MonoBehaviour and use an on scene GameObject that pools the collected items under itself. For duplicate Items I've devised a plan that uses a quantity modifier instead of pooling the same item twice or more.

Setting up any other solution seemed to go against the fundamental services Unity allowed me and solved my problem in the fastest manner.

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
1

Answer by ShadyProductions · Jun 29, 2017 at 04:57 PM

Add an itemtype enum orsomething on your item class that you can set?

Comment
Add comment · 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 tuncOfGrayLake · Jun 29, 2017 at 06:03 PM 0
Share

This feels like a roundabout way of addressing this situation and it necessitates either duplicate declaration of an enum of all available item types or a code that uses reflection to look up all types and automatically generates this content. Then I need to write cases for what the code should do for each type and so forth.

avatar image ShadyProductions tuncOfGrayLake · Jun 29, 2017 at 06:03 PM 0
Share

You are already over complicating it anyway.. Ins$$anonymous$$d of giving the item to the item container, give it an int value of the item's id, so whenever you collide with your container, you can have a method that looks up the object that has the given id inside the container, that way you can retrieve a generic object or whatever you want to retrieve. Or you can make every possible object inherit from some interface then you can do something like

 item : IDropable
 medkit : IDropable
 
 IDropable drop; //you can assign both item and medkit class
avatar image tuncOfGrayLake ShadyProductions · Jun 29, 2017 at 06:20 PM 0
Share

I think that's a good approach. I can simply store the ID in the ItemContainer and have a ScriptableObject of all available items and retrieve necessary info from thes places. The inventory of the player would simply have what the item ID is and the quantity for that item.

I had trouble understanding, however, what you meant by the example with IDroppable? Care to elaborate?

Edit: I was very happy with your suggestions and later on I tried another solution using Interfaces however it seemed to be again too much for too little. Just wanted to say thank you for your input. I appreciate it and glad you've shared your experience.

Show more comments

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

67 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

Related Questions

How do I retrieve an item from a list? 1 Answer

Managing Item Types 1 Answer

Item database with override functions 0 Answers

inventory system: drop item with mouseclick 1 Answer

Problem accessing enum from other class 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