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 /
avatar image
0
Question by DorkNemesis · Jun 12, 2011 at 11:21 AM · inventory

Inventory System - Find Empty Slot

Hey guys,

I'm currently designing an inventory system, but it's pretty complex stuff.

What I want to do is this:

If player picks up an item, the game should look through the 'inventory' gameObject (that has a lot of variables for each slot with states like 'item', 'isEmpty', etc.), find out if there's an empty slot left in the inventory and then set the picked up item to that empty slot.

The part where I have problems is in actually finding out through the script which inventory slot is still empty.

Say we have 10 slots, the first 5 are already filled with items, the 6th one is empty. The code should be like:

If item was picked up -> var freeSlot: Go through all the inventory slots starting at 1 and find the next one that's empty. -> if empty found: Set Item that was picked up to freeSlot -> if no empty found: Item can't be picked up.

I think I just lack some programming knowledge in how to ask this. I'm pretty sure I could get it to work with a ton of if/else statements, but that's probably not the smart way to do it. Any hints?

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

Answer by Anxo · Jun 12, 2011 at 11:37 AM

I am not sure if your slots are game Objects or variables but This can be done with a for loop like this. lets say its a string variable array.

 NextEmptySlot = FindEmptySlot();
 
 function FindEmptySlot(EmptySlot : String ){
 
      for (var i = 0; i < Slots; i++;
      {
          if(Slots[i] == "isEmpty")
          {
               return Slots[i];
          }
      }
      return Full;
 }

You cant copy and paste my code because I am no where where I could test it but that is the idea. The for Loop function is returning a string, you could also have it return a game object. It then goes thru your Slots array and checks if its string is equal to isEmpty. If so, it will return that Slot arrays index. If it went thru the entire loop and did not return any slot it will return full.

I hope that gets you on the right track.

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 DorkNemesis · Jun 12, 2011 at 05:44 PM 0
Share

Thanks already, but I still have a very hard time figuring out how to get it to work.

Basically, I have an inventory GameObject that has an inventory script assigned to it.

Inside of that inventory script I have 3 variables:

var hotbarSlot01Item : String = "none"; var hotbarSlot02Item : String = "none"; var hotbarSlot03Item : String = "none";

Now, I have another script that is executed when the player picks something up. Now I don't know how to make the for loop work with a string.

I just tried something like this:

inv = GameObject.Find("inventory"); //Find Inventory GameObject for(inv.GetComponent(inventory).hotbarSlotItem == "none") //Go through every hotbarSlotItem and find the one that's set to "none"

inv.GetComponent(inventory).hotbarSlotItem = "item";

Now, I know this is complete bullshit code, but I can't find any good answers on how the for loop works with Strings anywhere.

There are 10 hotbar icons, upon pick-up of an item, go through every single one starting at 01, check if it's full or not, return the value of the first one that's empty and set it to the string of the item that I just picked up.

avatar image
0

Answer by unity_kUz2xaqEZNSPPQ · Sep 09, 2018 at 11:08 PM

in my case my code when breaking the tree it quanha item drop chances, but I just managed to create new slot, what I want to do is select a empty slot and add new item

 foreach (ItemDropChance itemChance in dropChances)
             if (Random.value <= itemChance.probability)
                 player.inventory.Add(new ItemSlot(new Item(itemChance.item)));
             
     
 

,in my case my code when breaking the tree it quanha item drop chances, but I just managed to create new slot, what I want to do is select a empty slot and add new item

foreach (ItemDropChance itemChance in dropChances) if (Random.value <= itemChance.probability) player.inventory.Add(new ItemSlot(new Item(itemChance.item)));

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

FPS Weapon inventory script help 0 Answers

Inventory SlotID vs. RectPosition 0 Answers

Issues with Inventory script 0 Answers

Object being destroyed without GetKey Down 1 Answer

Inventory armor wielding proplem,How to convert from derived to base 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