Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Chiroptera · Oct 20, 2014 at 02:14 PM · guiinventoryicon

Inventory SlotID vs. RectPosition

I didn't know how to describe it better so the longer version: I have an inventory (slots-class displayed as buttons) and everything is fine with that except that the items I pick up are stored in the correct slot per code but displayed in the wrong one. I am having trouble finding the cause for that.

I have a List of slots that contain information about the position of the rect they will use in onGUI, a slotID (which is basically the index they have in the list) and an itemID. In a double-for-loop I create a grid of slots, the grid-coordinates are used to create the rect-positions (from bottom left to top right). The itemID is by default set to -1 which means that the slot is empty. If the mouse has been clicked, the script goes through the list of slots to find the one which contains the mouse-position. (I checked that and it works). If I pick up an item, the script searches the list of slots for the first empty slot (which is the slot 0, I checked that too and it works). Since the content of the slot-list has changed the gui is redrawn but the itemIcon is drawn in the top left (which is slot12) although the item is correctly saved in slot0.

I use one and the same list of slots for all those actions. I use the same way to go through the list (always a for-loop from 0 upwards) and I use the slotID to verify the found slot is the one I want. Everything I checked is correct but the icon is drawn on the wrongs spot nontheless.

Some relevant scriptparts:

The pick-up: int slotID = FindEmptySlot();

     if(slotID != -1)
     {
         Slots[slotID].itemID = item;
     }
     else
     {
         Debug.Log("Inventory is full");
     }

int FindEmptySlot() { int slotID = -1;

     for(int i = 0; i < Slots.Count; i++)
     {
         if(Slots[i].itemID == -1)
         {
             slotID = i;
             break;
         }
     }

     return slotID;
 }

The GUI: void InventoryWindow(int id) { Texture2D icon;

     for(int i = 0; i < Slots.Count; i++)
     {

         if(Slots[i].itemID != -1)
         {
             icon = itemData.Items[Slots[i].itemID].itemIcon;
             GUI.Button( Slots[i].slotRect, icon);
         }
         else
         {
             GUI.Button( Slots[i].slotRect, "");
         }
     }
 }

The input: if(Input.GetMouseButtonDown(0)) {
for(int a = 0; a < Slots.Count; a++) {

if(Slots[a].slotRect.Contains(Input.mousePosition)) { Debug.Log(Slots[a].slotID + " : " + Slots[a].itemID); break; } }

Doesn't look to nice sorry but my internet is freaking out right now, hope you can read it though. If you need more code to see whats happening please tell me and I will post that too. Would be great if anyone of you could help since I need to fix this til friday.

Comment
Add comment · Show 3
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 Em3rgency · Oct 20, 2014 at 02:53 PM 0
Share

Did you keep in $$anonymous$$d that a rect with position (0,0) is TOP LEFT? Adding to the X coordinate makes it go right, adding to the Y coordinate makes it go down. So if your inventory slot 0 is bottom left, its coordinate is (0,[screen-height]) .

Its not evident in the code, but I think this is your problem, judging solely from your description.

avatar image Chiroptera · Oct 20, 2014 at 03:13 PM 0
Share

duh that might be the problem. thank you I will fix it now. Sometimes I don`t see the obvious things cause I am thinking too complex or sth..thanks

avatar image Chiroptera · Oct 20, 2014 at 03:17 PM 0
Share

although if I click on a button it tells me it is slot0 when it should be slot0 and clicking on it is checked with the rect.contains function...shouldn't the icon be in the right rect aswell?

I will however change the rect-positions and hope that it works.

=> checked it and it works. I am a bit puzzled because of the thing above but maybe I will not $$anonymous$$d and just carry on. Thanks again.

0 Replies

· Add your reply
  • Sort: 

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

Populate inventory with item icons 1 Answer

Inventory, why didn't work element? 1 Answer

How to make a simple inventory?!? Beginner coder JS 1 Answer

GUI focus control - inventory system issue 1 Answer

New GUI and Inventory problem. 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