• 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 SterlingStudios · Jun 27, 2015 at 12:55 AM · javascriptinventoryloops

Inventory System Help

Hi, I'm making a RPG-like game, and I'd like some help on what's going wrong on my inventory system. Right now, it kind of works. What I mean is sometimes it will work fine, but other times it won't at all. Basically, sometimes when I click the pickup item it registers and goes into the inventory, and it fills the spot and checks the boolean. But sometimes it requires both items in the hierarchy to be active for the boolean to be true. Here is the inventory space code:

 #pragma strict
 
 var isEmpty : boolean;
 
 var items : GameObject[];
 
 function Update () {
 
     for(var i = 0; i < items.Length; i++) {
         if(items[i].activeInHierarchy) {
             break;
             isEmpty = false;
         }
     
         else {
             isEmpty = true;
         }
         
         return;
     }
 }

And the item pickup code:

 #pragma strict
 
 var inventorySpace : InventorySpace[];
 
 var objs : GameObject[];
 
 function OnMouseDown () {
 
     for(var i = 0; i < inventorySpace.Length; i++) {
         if(inventorySpace[i].isEmpty) {
             objs[i].active = true;
             inventorySpace[i].isEmpty = false;
             break;
         }
     }
 }

Any help would be greatly appreciated!

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

2 Replies

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

Answer by FaizanDurrany · Jun 27, 2015 at 04:16 PM

the problem i see is:

  1. you are using break before making isEmpty false

  2. and you are returning as soon as the if statement is finished

check this code and see if it runs

 #pragma strict
  
  var isEmpty : boolean;
  
  var items : GameObject[];
  
  function Update () {
      for(var i = 0; i < items.Length; i++) {
          if(items[i].activeInHierarchy) {
              isEmpty = false;
              break;
          }
      
          else {
              isEmpty = true;
          }
      }
  }
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 SterlingStudios · Jul 01, 2015 at 07:37 PM 0
Share

Wow, I feel really dumb, haha. Well, thank you for helping me.

avatar image
0

Answer by calmcarrots · Jun 27, 2015 at 06:35 AM

Switch lines 11 and 12 in your first script. When the code hits the 'break' code, it wont reach everything else that is after it. This might be your problem.

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 SterlingStudios · Jun 27, 2015 at 01:32 PM 0
Share

It didn't work, seems like it does the same thing.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

I need some help on inventory. 0 Answers

My inventory system only picks up one item when there are several 1 Answer

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

Inventory Help. 2 Answers

Changing Variable OnMouseClick? Help! 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