Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 31, 2015 at 03:55 PM by ThePokedog1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ThePokedog1 · Aug 19, 2015 at 08:33 PM · c#inventoryinventory system

How do I make my item only open if there is room in my Inventory?

I am trying to only opening an item if there is room in my inventory. Here is a segment of my code.

 if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
                         {
                             if(item.itemType == Item.ItemType.Consumable)
                             {
                                 UseConsumable(slots[i],i);
 
                             }
                         }

//////////////////////////////////////Segment//////////////////////////////////

 void UseConsumable(Item item, int slot)
     {
         switch(item.itemID)
         {
             case 22:
             {
                 inventory[slot] = new Item();
                 AddItem(20);
                 AddItem(23);
                 AddItem(24);
                 AddItem(25);
                 break;    
             }
 
         }
         
     }
                 
     void RemoveItem(int id)
     {
         for(int i = 0; i < inventory.Count; i++)
         {
             if(inventory[i].itemID == id)
             {
                 inventory[i] = new Item();
                 break;
             }
         }
     }
 
 
 
     void AddItem(int id)
     {
 
         for(int i = 0; i< inventory.Count; i++)
         {
             if(inventory[i].itemName == null)
             {
                 for(int j = 0; j < database.items.Count; j++)
                 {
                     if(database.items[j].itemID == id)
                     {
                         inventory[i] = database.items[j];
                     }
                 }
                 break;
 
             }
 
 
         }
     }
 
     bool InventoryContains(int id)
     {
         for (int i = 0; i < inventory.Count; i++)
             if (inventory[i].itemID == id) return true;
         return false;
     }

I included the parts after use consumable so you can have a better idea. I tried doing an if statement but I can't even think what I would put for it to get this to work. The overall goal is to only open the my item if there is four slots of inventory available. Any Ideas?

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 ThePokedog1 · Aug 19, 2015 at 05:20 PM 0
Share

I can do if(InventoryContains(-1)){ to get it to only open if there is one slot of nothing but I can't figure out how to make it multiple slots.

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by ThePokedog1 · Aug 19, 2015 at 10:20 PM

Got it to work! Did this instead inside the GetInventorySpace()

 if(inventory[i].itemID == -1) 
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
avatar image
1

Answer by Maui-M · Aug 19, 2015 at 09:40 PM

You could add a new function to find the count of empty slots:

  int GetInventorySpace()
  {
      int count = 0;
      for (int i = 0; i < inventory.Count; i++)
          if (inventory[i].itemID == -1) count += 1;
      return count;
  }

Then just check if GetInventorySpace() >= 4. The other option would be to have a variable change when you add or remove items from your inventory.

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
avatar image
1

Answer by NeverHopeless · Aug 19, 2015 at 10:03 PM

You may try this:

 if(item.itemType == Item.ItemType.Consumable && i < slot.Length - 4)
 {
     UseConsumable(slots[i],i);
 }

Here i < slot.Length - 4 will prevent to go in this condition there are less than 4 slot available.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

unity3d simple inventory 1 Answer

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

[C#] Grouping variables 1 Answer

adding inventory items is half-working 0 Answers

Object Reference Not Set To An Instance of An Object Error 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