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 /
  • Help Room /
avatar image
0
Question by Bokaii · Jan 17, 2016 at 12:57 PM · c#listinventory systemcrafting

Inventory Add Item error

So I wrote an inventory script, based on a list to store my items in. I've made a function to add the item, where I pass in an id, and an amount. In the function I check multiple things. First I check if I even have anything in my inventory, if I don't, it's easy. I just add the given item as the very first item in my list. Second I check if I already have the item in my inventory and if it's stackable. I do that with a bool function:

 bool doesExist(int id)
     {
         for(int i = 0; i < inventoryCount; i++)
         {
             if(inventory[i].itemID == id)
             {
                 existingSlot = i;
                 return true;
             }
         }
         //existingSlot = inventoryCount -= 1;
         //Reset inventoryCount
         return false;
     }

If I already have one of these items in my inventory and it's stackable, I just add the given amt to the total amount for that inventory slot. If however, I do not have this item in my inventory, or it's just not stackable, I have an else statement. In the else statement I add a new item with the given id and amount. It all works great.

But... I made a crafting system, where I find a recipe (premade), and then I loop through my inventory to check if I have all the item. If I do, I call the addItem function. This all works great the first time, but the second time I try to craft, it adds the item with the id one below (so say if I craft an axe with the id of 4, second time I craft the axe, it adds item with id of 3)

Crafting System: public void SelectRecipe(string name) { for(int i = 0; i < recipes.recipes.Length; i++) { if(recipes.recipes[i].name == name) { selectedRecipe = recipes.recipes[i].recipe; index = i; //print(selectedRecipe[0]); CheckforItems(); break; } } }

     void CheckforItems()
     {
         //print("Checking for items");
         //print(inv.inventory[inv.inventoryCount-=1].itemName);
 
         string[] first = selectedRecipe[0].Split(';');
         string[] second = selectedRecipe[1].Split(';');
         string[] third = selectedRecipe[2].Split(';');
 
 
         for (int i = 0;i < inv.inventoryCount; i++)
         {
 
             if (inv.inventory[i].itemName == first[0] && inv.inventory[i].itemAmt >= int.Parse(first[1]))
             {
                 firstSlot = i;
                 requiredFirst = true;
             }
 
             if (requiredFirst && inv.inventory[i].itemName == second[0] && inv.inventory[i].itemAmt >= int.Parse(second[1]))
             {
                 secondSlot = i;
                 requiredSecond = true;
             }
             if(requiredSecond && inv.inventory[i].itemName == third[0] && inv.inventory[i].itemAmt >= int.Parse(third[1]))
             {
                 //Remove first item(s)
                 inv.inventory[firstSlot].itemAmt -= int.Parse(first[1]);
                 //inv.slots[firstSlot].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[firstSlot].itemAmt.ToString();
 
                 //Remove second item(s)
                 inv.inventory[secondSlot].itemAmt -= int.Parse(second[1]);
                 //inv.slots[secondSlot].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[secondSlot].itemAmt.ToString();
 
                 //Remove third item(s)
                 inv.inventory[i].itemAmt -= int.Parse(third[1]);
                 //inv.slots[i].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = inv.inventory[i].itemAmt.ToString();
 
                 //Add Item to inventory
                 AddItemToInv(recipes.recipes[index].id);
                 //print(recipes.recipes[index].id);
             }
         }
     }
 
 
 
 
     void AddItemToInv(int id)
     {
         //print(id);
         inv.AddItem(id, 1);
 
         requiredFirst = false;
         requiredSecond = false;
         firstSlot = 0;
         secondSlot = 0;
         thirdSlot = 0;
     }


Inventory addItem function: public void AddItem(int id, int amt) {

         //If we don't have anything in our inventory
         if(inventory.Count == 0)
         {
             inventory.Add(database.items[id -= 1]);
             inventory[0].itemAmt = amt;
 
             //Visual
             slots.Add(Instantiate(slotPrefab));
             slots[0].transform.SetParent(inventorySlots.transform);
 
             GameObject itemObj = Instantiate(itemPrefab);
             itemObj.transform.SetParent(slots[0].transform);
             itemObj.GetComponent<Image>().sprite = database.items[id].itemIcon;
             //Reset id
             id = 0;
             //print(id);
 
             itemObj.transform.GetChild(0).GetComponent<Text>().text = inventory[0].itemAmt.ToString();
 
             return;
         }
 
         //If we already have one of these items in our inventory
         if (doesExist(id) && database.items[id-=1].stackAble)
         {
             print(id);
             inventory[existingSlot].itemAmt += amt;
 
             //Visual
             slots[existingSlot].transform.GetChild(0).GetChild(0).GetComponent<Text>().text = inventory[existingSlot].itemAmt.ToString();
 
             id = 0;
 
             return;
         }
         else
         {
             //print(id);
             inventory.Add(database.items[id -= 1]);
             //Reset id
             //id += 1;
             inventory[inventoryCount].itemAmt = amt;
             //print(database.items[id].itemName);
 
             //Visual
             slots.Add(Instantiate(slotPrefab));
             slots[inventoryCount].transform.SetParent(inventorySlots.transform);
 
             GameObject itemObj = Instantiate(itemPrefab);
             itemObj.transform.SetParent(slots[inventoryCount].transform);
             itemObj.GetComponent<Image>().sprite = database.items[id].itemIcon;
             //print(id);
             //Reset id
             id = 0;
 
             itemObj.transform.GetChild(0).GetComponent<Text>().text = inventory[inventoryCount].itemAmt.ToString();
 
             return;
         }
     }

The error seems to be located in the last part of the addItem function. The id changes, and I have no idea why. Can anyone help me pls? :3

Comment
Add comment · Show 2
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 Danixos · Jan 17, 2016 at 01:04 PM 0
Share

In your AddItem() function, you decrement id in several places (i.e. id -= 1). Are you sure that this is what you want? Shouldn't it be id - 1?

avatar image Bokaii Danixos · Jan 17, 2016 at 01:23 PM 0
Share

This worked... omg thank you, I did not think of that at all. Haha I had a derp moment :P

2 Replies

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

Answer by Bokaii · Jan 17, 2016 at 01:28 PM

So like you said in the first comment, I used id -= 1, where I was supposed to use id - 1. This fixed it.

Comment
Add comment · Show 1 · 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 Dudicus · Jan 17, 2016 at 01:55 PM 0
Share

I agree!!!

avatar image
0

Answer by Danixos · Jan 17, 2016 at 01:11 PM

Line 24: you're decrementing id in if header. So even if the condition is false and else section is executed, id will be off by one.

Comment
Add comment · Show 1 · 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 Bokaii · Jan 17, 2016 at 01:13 PM 0
Share

So how would I correct this?

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

54 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

Related Questions

Need help with 3d inventory system 0 Answers

List instance is always the same as the blueprint ? 0 Answers

c# foreach does not work 0 Answers

Removing objects with children from lists 0 Answers

Removing item from list makes it appear at the bottom ? 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