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 Olgo · Jun 18, 2013 at 03:24 PM · inventoryvaluefor-loopinventory system

Not all code paths return a value?

Hello,

This is probably an easy one, but I'm not a very experienced programmer. I understand what the error "Not all code paths return a value" means, but I don't know why I would get it in this case.

I'm creating an inventory system for an RPG from scratch, I have the following function to look for a null space in the inventory array, if it is null, put Item in it. The function will then return a string "Added Item" or "Inventory full." What's wrong here?

             public string AddItem(Item addedItem){
                 for(int i = 0; i < baseInvSize; i++){
                     if(playerInventory[i] == null){
                         playerInventory[i] = addedItem;
                         return "added";
                     }
                     else
                         return "Inventory Full";
                 }
              }
Comment
Add comment · Show 1
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 XGundam05 · Jun 18, 2013 at 03:26 PM 0
Share

First thing to note, if you're inventory array has 1 item in the first slot and 5 blank slots afterwards, your code reports the inventory as full.

1 Reply

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

Answer by XGundam05 · Jun 18, 2013 at 03:32 PM

First, you are really only checking the first item in the array with the way you've structured your if statement.

Second of all, as baseInvSize is a variable, it's entirely possible for your loop to never be entered.

So really, you want to loop through the array and if you find a null position, add the item and return added (I'd also use a boolean, but that's irrelevant at this point). If you get through the entire loop and nothing is found, then you return Inventory Full.

So:

 public string AddItem(Item addedItem){
     for(int i = 0; i < baseInvSize; i++){
         if(playerInventory[i] == null){
             playerInventory[i] = addedItem;
             return "added";
         }
     }

     return "Inventory Full";
 }
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 Olgo · Jun 18, 2013 at 03:59 PM 0
Share

Gah! I see. I had my blinders on. I didn't realize that else statement was the downfall of my for loop. Thanks man.

avatar image Olgo · Jun 18, 2013 at 04:05 PM 0
Share

p.s. thanks for the tip about the bool, i've changed that as well.

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

15 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

Related Questions

Array index is out of range? 1 Answer

unity3d simple inventory 1 Answer

How can I delete an item from this inventory system? 1 Answer

Inventory Help. 0 Answers

A way to implement "Use" of item in game. A way to serialize object with method from other script maybe? 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