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
1
Question by MarkPwns1 · Oct 12, 2014 at 04:37 AM · c#arrayinventoryrpgitem

Can't Assign Item In Array

So I've been working on an inventory script, I'm trying to test it out. Everything works fine except for the AddItem method, shown below.

     public void AddItem(Item item) {

             // Check if a stack is already there for us to stack the item with
         for(int i = 0; i <= 25; i++) {

                     // If nothing is in this inventory slot, check the next one.
             if(Items[i] == null) {
                 continue;
             }

                     // If stack is found, increase the stack size.
             if(Items[i].ContainingItem.Name == item.Name) {
                 Items[i].StackSize++;
                 return;
             }
             
         }

             // Create a new item stack.
         ItemStack st = new ItemStack(item, 1);

             // If no stacks of the same item is found, then
             // check for space in inventory to put a new item stack into.
         for(int i = 0; i <= 25; i++) {
                     // If item in inventory slot is null (no itemstack inside)
             if(Items[i] == null) {
                             // Add a new item stack.
                 Items[i] = st;
                 return;
             }
         }
         
         /*foreach(ItemStack i in Items) {
             Debug.Log(i.ContainingItem.Name);
         }*/
     }

Here's my problem: whenever I add an item, it adds nothing, and the Debug.Log stuff doesn't print anything, suggesting that nothing is being added to the array. Oddly enough, if I remove the 2 return statements the entire inventory is litterally filled with nothing but that item. Will somebody please fix this, or atleast explain why this is happening?

EDIT: I've only included the AddItem method, I assure you everything else is working as expected.

EDIT: Items is a built-in array, I thought to use this because it would work very well with the inventory slots stuff. ItemStack is a class with 3 properties: ContainingItem, StackSize, and MaxStackSize. Stacksize is just a public variable (I know, it's bad, I'm sorry :P)

When an item that doesn't exist in inventory is added into a non-full inventory it should just add that item.

When an item that does exist in inventory is added it should increase stack size, I haven't checked if that works yet.

When the inventory is full, the item is dismissed and never seen from again. If you want the entire script here it is, but it's a bit huge and messy, I'll try to comment as much as I can.

Pastebin link to my inventory script is here: http://pastebin.com/kxupp6Ap

Item here: http://pastebin.com/YwB6ru29

and ItemStack here: http://pastebin.com/4YRSc5Bz

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
1

Answer by Bunny83 · Oct 12, 2014 at 04:53 AM

Some more background would help:

  • Is Items a built-in array or a container class like List?

  • Is ItemStack a class or a struct?

  • Is StackSize an int-field or an int-property (or maybe something else?)

What things you have already tried to debug your problem? Where have you put Debug.Logs, what did you log and what was the result?

In general there are 3 different cases when you add an item:

  • You add a new item that doesn't yet exists in the inventory. The inventory is not full.

  • You add an item that does already exist in the inventory

  • You try add an item that doesn't yet exists, but the inventory is full.

With which case you have problems and what's the exact result when you add the item? How far does the method run? Debug.Log?

With the information given it's near to impossible to figure out what might be wrong. The given code looks solid except that the full inv situation isn't handled at all and the item would just vanish.

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 MarkPwns1 · Oct 14, 2014 at 09:24 PM 0
Share

Items is a built-in array, I thought to use this because it would work very well with the inventory slots stuff. ItemStack is a class with 3 properties: ContainingItem, StackSize, and $$anonymous$$axStackSize. Stacksize is just a public variable (I know, it's bad, I'm sorry :P)

When an item that doesn't exist in inventory is added into a non-full inventory it should just add that item.

When an item that does exist in inventory is added it should increase stack size, I haven't checked if that works yet.

When the inventory is full, the item is dismissed and never seen from again. If you want the entire script here it is, but it's a bit huge and messy, I'll try to comment as much as I can.

Pastebin link to my inventory script is here: http://pastebin.com/kxupp6Ap

Item here: http://pastebin.com/YwB6ru29

and ItemStack here: http://pastebin.com/4YRSc5Bz

So please help me! >_< I also practically spammed pastebin while doing this.

avatar image Bunny83 · Oct 15, 2014 at 01:06 AM 0
Share

@$$anonymous$$arkPwns1: Everything works as it should except one small thing ;) Is it possible that you are used to work with indices that start at 1? Anyways, your problem isn't the adding of the item, but your loop that displays the items. You initialize "buttonnum" with 0 but you immediately increase it by one. That means you effectively are skipping the first item (index 0)

You should get used to 0 based indices since most languages use 0-based indices (for several reasons).

Your Items array seems to be too large. It holds 26 items ins$$anonymous$$d of 25, that's why you didn't get an index-out-of-bounds exception on the last element since your index is off by 1

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Scroll List Problem 1 Answer

Real Simple Inventory and Vendor System Help 1 Answer

Array Overflow Problem 1 Answer

C# Adding to an Array 1 Answer

Preventing Items from shifting in a list 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