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 hyosun0 · May 13, 2019 at 08:54 AM · c#2dinventoryinventory systemstorage

chest items get added to inventory

i've had this problem for some time with my chests

lets say i store 3 wood in the chest, close it and gather another 2, instead of having 2 in my inventory and 3 in the chest i have all 5 in my inventory

i followed a tutorial for both the inventory and chest system, but i changed the inventory system alot so i cant just redo the tutorial

i tried to fix this for some time, i think i found the thing that wrent wrong but i cant figure out how to fix it


heres all necessary code (unless im forgetting something) with notes added

https://hatebin.com/vlqyogayuy


heres what it looks like in action

https://i.imgur.com/ySAHyKD.gifv

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
2

Answer by Tom4nt · Jun 02, 2019 at 03:42 PM

So as understood, you have an inventory for your player and other for your chest. I think the best way of storing the items is using the Script in the chest that contains a List of items and other in the player, also with a list of items.

As you open the chest, you can access the List on the Chest object (via script) and transfer the items you want form it to the List on your player script. Then, when you close the chest, the items on it are independent but still there.

You can use

yourList1.Remove(item);
to remove at item from a list and then
yourList2.Add(item);
to add it to the other list.

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 hyosun0 · Jun 03, 2019 at 01:28 PM 0
Share

but i have that already no?

isn't the problem with

     public void AddItems()
     {
         if (chestItems != null)
         {
             foreach (Item item in chestItems)
             {
                 item.$$anonymous$$ySlot.AddItem(item);
             }
         }
     }

in chest because the script doesnt know that item.myslot refers to the chest and not in general? i dont know how to distinguish between chest and inventory slots

i tried this

     public void AddItems()
     {
         if (chestItems != null)
         {
             foreach (Item item in chestItems)
             {
                 if (item.$$anonymous$$ySlot.isChest)
                 {
                     item.$$anonymous$$ySlot.AddItem(item);
                 }
                 
             }
         }
     }
 
 //In ChestBag
 
     public void AddChestSlots(int slotCount)
     {
         for (int i = 0; i < slotCount; i++)
         {
             SlotScript slot = Instantiate(slotPrefab, transform).GetComponent<SlotScript>();
             slot.$$anonymous$$yIndex = i;
             slot.isChest = true;
             $$anonymous$$ySlots.Add(slot);
         }
     }

but now this happens

https://i.imgur.com/40XGZSV.gifv

avatar image Tom4nt hyosun0 · Jun 03, 2019 at 03:55 PM 1
Share

As I see on your code, your item is referencing a slot in the UI, but I think you should do it on the other way around. If a slot references an item, you can easily set the item from the list for a specific slot in the player or chest inventory. I don't know what properties you have on your Item script but you can have a property to store the quantity of the item and display it in the slot. Also your item image is being stored in the slot, witch doesn't make total sense, as you should store it on the item itself.

Also, do you have a list of Items on your wooden source/pot? Because it is basically a chest at this point and it makes sense that you have one.

I'm sorry if I got something wrong. I'm a bit confused by your code. I would use a different approach to obtain this result.

avatar image
0

Answer by hyosun0 · Jun 04, 2019 at 11:10 PM

thanks for helping @Tom4nt , but im not quite sure if i know what to do to change/fix it since im fairly new

maybe i should just skip the storage system for now and come back once i know more? although i feel like this shouldnt be too hard to fix, i just cant figure out how

the pot is weird, i tried to make it with stacks but it didnt really work so i had to make it like this

https://i.imgur.com/mL5hP0k.png

 public class LootTable : MonoBehaviour
 {
     public Loot[] loot;
     public List<Item> droppedItems = new List<Item>();
     private Item tmp;
     private bool rolled = false;
     public bool opened = false;
 
     private void Start()
     {
         droppedItems.Add(tmp);
     }
 
     public void AddLoot()
     {       
         if (!rolled)
         {
             RollLoot();
         }
         droppedItems.Remove(tmp);
         LootWindow.MyInstance.items.Clear();
         LootWindow.MyInstance.items.AddRange(droppedItems);
         LootWindow.MyInstance.itemCount = droppedItems.Count;     
         LootWindow.MyInstance.AddLoot();
     }
 
     public void RollLoot()
     {
         foreach (Loot item in loot)
         {
 
             int roll = Random.Range(0, 100);
             if(roll <= item.MyDropChance)
             {
                     droppedItems.Add(item.MyItem);             
             }
         }
         rolled = true;
     }
 }


my item class is this

 [CreateAssetMenu]
 public abstract class Item : ScriptableObject, IMoveable, IDescribeable
 {
     [HideInInspector]
     public SlotScript slot;
     public BagScript bag;
     public Sprite itemSprite;
     [SerializeField]
     private string itemName;
     [Range(1, 64)] public int maxStack = 64;
     [SerializeField]
     private Quality quality;
 
     public Sprite MyIcon
     {
         get
         {
             return itemSprite;
         }
     }
 
     public int MyMaxStack
     {
         get
         {
             return maxStack;
         }
     }
 
     public BagScript MyBag
     {
         get
         {
             return bag;
         }
         set
         {
             bag = value;
         }
     }
 
     public SlotScript MySlot
     {
         get
         {
             return slot;
         }
         set
         {
             slot = value;
         }
     }
 
     public Quality MyQuality { get => quality;}
     public string MyItemName { get => itemName;}
 
     public virtual string GetDescription()  
     {
         return string.Format("<color={0}>{1}</color>\n",  QualityColor.MyColors[MyQuality], MyItemName);    
     }
 
     public void Remove()
     {
        if(MySlot != null)
         {
             MySlot.RemoveItem(this);
         }
     }
 
 }


with the items just inheriting from it (like wood (just fuel as of now) below)

 [CreateAssetMenu(fileName = "Fuel", menuName = "Items/Fuel", order = 4)]
 public class Fuel : Item
 {
     [SerializeField]
     private bool isFuel;
     [SerializeField]
     private float fuel;
 
     public float MyFuel
     {
         get
         {
             return fuel;
         }
     }
 
     public bool IsFuel
     {
         get
         {
             return isFuel;
         }
     }
     public override string GetDescription()
     {
         return base.GetDescription() + string.Format("\nFuel amount: {0}", MyFuel);
     }
 }
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

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

106 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

Related Questions

unity3d simple inventory 1 Answer

My script for pick up items work is wrong! Help please 1 Answer

Object Reference Not Set To An Instance of An Object Error 1 Answer

adding inventory items is half-working 0 Answers

Inventory AddItem help 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