Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 stuart6854 · Jan 02, 2014 at 05:53 PM · listadding

Nested If Addition Going wrong somewhere?

I have nested if's in side a for loop which is used to add an item to a list. But when i first add an item with an amount of 4 an then add a second item with 3 it should make 7, but come out with 6?

I tried putting 'print()' functions around the function, but i couldnt see anything wrong?

Code:

 public void AddItem(Item newItem){
         int itemAmount = newItem.currentStack;
         if(PlayerInventory.Inventory.Count > 0){
             foreach(Item item in PlayerInventory.Inventory){
                 if(item.ID == newItem.ID){    
                     if(item.currentStack < item.maxStack){
                         int availableSpace = item.maxStack - item.currentStack;
                         if(itemAmount <= availableSpace){
                             print (item.currentStack + itemAmount + " :Final Amount");
                             item.currentStack += itemAmount;
                             itemAmount = 0;
                         } else{
                             item.currentStack = item.maxStack;
                             itemAmount -= availableSpace;
                         }
                     }
                 }
             }
             if(itemAmount > 0){
                 newItem.currentStack = itemAmount;
                 PlayerInventory.Inventory.Add(newItem);
             }
         } else{
             PlayerInventory.Inventory.Add(newItem);
         }
         print ("-------------------------");
     }    

Comment
Add comment · Show 8
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 HappyMoo · Jan 02, 2014 at 06:51 PM 0
Share

try to use a

 Dictionary<int,Item>

that maps your item.ID to an Item object.. that way you don't need to loop through the list.

I can help you later if you don't get it fixed or no one else helps.

avatar image stuart6854 · Jan 02, 2014 at 06:55 PM 0
Share

I wanna stick to lists as they seem easier to handle(I was also using a dictionary earlier for a list of 'preset' items, but it wouldn't work for what i wanted to do). As with my problem i do not know what is wrong, its like unity is adding the new amount to the current stack wrong because i have checked and the function keeps the right numbers up to the addition?

avatar image HappyMoo · Jan 02, 2014 at 08:35 PM 0
Share

Can you show the code for Item and PlayerInventory for some context?

And where you initialize and add items.

avatar image stuart6854 · Jan 02, 2014 at 10:10 PM 0
Share

Added everything(I think) you asked for in O.P.

avatar image robhuhn · Jan 03, 2014 at 09:35 AM 0
Share

I'm a bit confused by your code. At first you assign the value of newItem.currentStack

 int itemAmount = newItem.currentStack;

and then you check if the current item of the loop is equal to the new item

 if(item.ID == newItem.ID){...

So within this condition item is equal to newItem and item.currentStack is equal to itemAmount

 print (item.currentStack + itemAmount + " :Final Amount");

That would result in 6 if you chop a tree with amountGiven = 3 and result in 8 if the amountGiven of rock is 4.

$$anonymous$$ay be you want to add something like previousItem.currentStack to itemAmount?

@edit: nvm, I mixed up PlayerInventory.Inventory and PlayerInventory.ItemDictionary

Show more comments

1 Reply

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

Answer by HappyMoo · Jan 03, 2014 at 07:59 PM

Hi,

the answers site limits the length of what I can say here, so I took this to the forums.

Please have a look here: http://forum.unity3d.com/threads/220674

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 stuart6854 · Jan 03, 2014 at 08:07 PM 1
Share

Reading it Now, Thanks.

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

20 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

Related Questions

A node in a childnode? 1 Answer

Add Object to List of GameObjects C# 1 Answer

Cant instantiate from one list to another? 2 Answers

How to add item to Script Written in C# ? 1 Answer

List or Array for Rays 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