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 Malwallace12 · Jul 23, 2013 at 02:44 AM · javascriptinventorystackable

I need help fixing my inventory sorting script.

Basically, this is part of a script that lets me stack items of the same name within each item type list. The problem is, when I first add an item to the curInventory, it causes the size of the stack added to double because it is counted twice.

     function stackItems(){
     var lastItemIndex = curInventory.Count - 1; //the index of the last item in the list
     for(var i: int = 0; i < curInventory.Count;i++){ //for all items in the list
             
         if(curInventory[lastItemIndex].name == curInventory[i].name){ //if the name of the last item in the list is the same as the name of the 
                                                                       //current item being checked
             
             if (curInventory[lastItemIndex].stackable == true){ //if the item is able to be stacked
             
                 if(curInventory[i].stackCount < curInventory[i].maxStack){    //if the number of items currently in the stack is
                                                                                           //less than the max number of items able to stack
                     if(
                     curInventory[i].stackCount += curInventory[lastItemIndex].stackCount; //increase the stack count of the current item by 
                                                                                           //the number of items in the new stack. This is what causes new items to be counted twice.
                      
                      if (lastItemIndex !=0){
                      curInventory.RemoveAt(lastItemIndex);
                      }
                 }
             }
         }
     }
 }


I could really use a fresh set of eyes to help me figure out how I could adjust my current script so that it doesn't count items added to a blank curInventory twice.

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 aceronn · Apr 09, 2014 at 04:08 AM

I know this is prob not gonna get read, you have it going 1, then when stacked its taking the 1 and adding 1, which makes 2, but when it stacks again its not registering as 2 + 1, its going to register as 2 + 2. You need to do something like

stackcount++

then itll register as 1 + 1 = 2, 2+1 = 3 and so on. Sorry for messed up format, first answer on this xD

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Where can i begin to learn how to make an inventory. (Javascript). 1 Answer

Inventory Help. 2 Answers

I'm trying to add items to an inventory. 1 Answer

Inventory script Help Please 2 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