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 Bincredible · May 29, 2015 at 04:08 PM · arrayinventoryitemfor loop

Inventory moving items bug.

Hello, I have have recently created an inventory system and I have came across a bug while trying to add a way of moving items to other slots inside the inventory. The problem is that if I pick up an object, and then move it to another slot, and then pick up another object and move it, duplicates are created of the item being moved. However if I pick up all of the object and then once they are all in the inventory start to move them, there are no issues at all. I have been playing around with some solutions but I can't get anything to work. I made a video with the error to make it more clear with what I'm saying and I will also show the inventory code. Thanks for any help!

Link to the video showing the problem: https://www.youtube.com/watch?v=C11JloVYxtA&feature=youtu.be

Inventory script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
 
     public Item[] items;
     public Item[] itemsToUse;
     public Item emptyItem;
     public Item currentlySelected;
 
     private string _CURRENT_METHOD = "";
     private bool _CAN_MOVE_ITEM;
     private bool _DRAW_TEXTURE;
     private bool canPlaceItem;
 
     private bool _INVENTORY_OPEN;
     private const int _INVENTORY_WINDOW_ID = 0;
     private Rect _INVENTORY_WINDOW_RECT = new Rect(10,Screen.height-420,265,370);
 
 
     private void FixedUpdate(){
         //Clamp the window to the screen
         _INVENTORY_WINDOW_RECT.x = Mathf.Clamp (_INVENTORY_WINDOW_RECT.x, 0, Screen.width - _INVENTORY_WINDOW_RECT.width);
         _INVENTORY_WINDOW_RECT.y = Mathf.Clamp (_INVENTORY_WINDOW_RECT.y, 0, Screen.height - _INVENTORY_WINDOW_RECT.height);
 
         //Check to see if any slots have an empty name, if so, the slot must be empty. Likewise if the slot's name is not empty, the slot can't be empty
         for (int i = 0; i < 20; i ++) {
             if(items[i].itemName == ""){
                 items[i].empty = true;
             }else if(items[i].itemName != ""){
                 items[i].empty = false;
             }
         }
 
     }
 
     void OnGUI(){
         if(GUI.Button(new Rect(10,Screen.height-40,120,30),_INVENTORY_OPEN ? "Close Inventory" : "Open Inventory")){
             _INVENTORY_OPEN = !_INVENTORY_OPEN;
         }
         if (_INVENTORY_OPEN) {
             _INVENTORY_WINDOW_RECT = GUI.Window(_INVENTORY_WINDOW_ID, _INVENTORY_WINDOW_RECT, InventoryWindow, "Inventory");
         }
         if (_DRAW_TEXTURE) {
             Event mousePos = Event.current;
             GUI.DrawTexture(new Rect(mousePos.mousePosition.x, mousePos.mousePosition.y, 50, 50), currentlySelected.itemIcon, ScaleMode.ScaleToFit);
             _CURRENT_METHOD = "PlaceItem";
         }
     }
 
     void InventoryWindow(int windowId){
         //Begin the group of inventory buttons
         GUI.BeginGroup (new Rect (10, 25, _INVENTORY_WINDOW_RECT.width - 20, _INVENTORY_WINDOW_RECT.height - 40));
         //Buttons for each item in the inventory
         if (GUI.Button (new Rect (0, 0, 50, 50), items[0].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[0];
                 _DRAW_TEXTURE = true;
                 items[0] = emptyItem;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[0] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (60, 0, 50, 50), items[1].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[1];
                 items[1] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[1] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (120, 0, 50, 50), items[2].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[2];
                 items[2] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[2] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (180, 0, 50, 50), items[3].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[3];
                 items[3] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[3] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (0, 60, 50, 50), items[4].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[4];
                 items[4] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[4] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (60, 60, 50, 50), items[5].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[5];
                 items[5] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[5] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (120, 60, 50, 50), items[6].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[6];
                 items[6] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[6] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (180, 60, 50, 50), items[7].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[7];
                 items[7] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[7] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (0, 120, 50, 50), items[8].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[8];
                 items[8] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[8] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (60, 120, 50, 50), items[9].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[9];
                 items[9] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[9] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (120, 120, 50, 50), items[10].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[10];
                 items[10] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[10] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (180, 120, 50, 50), items[11].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[11];
                 items[11] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[11] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (0, 180, 50, 50), items[12].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[12];
                 items[12] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[12] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (60, 180, 50, 50), items[13].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[13];
                 items[13] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[13] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (120, 180, 50, 50), items[14].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[14];
                 items[14] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[14] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (180, 180, 50, 50), items[15].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[15];
                 items[15] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[15] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (0, 240, 50, 50), items[16].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[16];
                 items[16] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[16] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (60, 240, 50, 50), items[17].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[17];
                 items[17] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[17] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (120, 240, 50, 50), items[18].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[18];
                 items[18] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[18] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
         if (GUI.Button (new Rect (180, 240, 50, 50), items [19].itemIcon)) {
             if(_CURRENT_METHOD == "MoveItem"){
                 currentlySelected = items[19];
                 items[19] = emptyItem;
                 _DRAW_TEXTURE = true;
             }
             else if(_CURRENT_METHOD == "PlaceItem"){
                 items[19] = currentlySelected;
                 currentlySelected = emptyItem;
                 _DRAW_TEXTURE = false;
                 _CURRENT_METHOD = "";
             }
         }
 
         //Method Buttons
         if (GUI.Button (new Rect (0, 300, 70, 25), "Move")) {
             _CURRENT_METHOD = "MoveItem";
         }
         if (GUI.Button (new Rect (80, 300, 70, 25), "Use")) {
             _CURRENT_METHOD = "UseItem";
         }
         if (GUI.Button (new Rect (160, 300, 70, 25), "Delete")) {
             _CURRENT_METHOD = "DeleteItem";
         }
 
         //Labels for amounts
         GUI.Label (new Rect (33, 30, 30, 30), items [0].amount.ToString());
         GUI.Label (new Rect (93, 30, 30, 30), items [1].amount.ToString());
         GUI.Label (new Rect (153, 30, 30, 30), items [2].amount.ToString());
         GUI.Label (new Rect (213, 30, 30, 30), items [3].amount.ToString());
         GUI.Label (new Rect (33, 90, 30, 30), items [4].amount.ToString());    
         GUI.Label (new Rect (93, 90, 30, 30), items [5].amount.ToString());    
         GUI.Label (new Rect (153, 90, 30, 30), items [6].amount.ToString());    
         GUI.Label (new Rect (213, 90, 30, 30), items [7].amount.ToString());    
         GUI.Label (new Rect (33, 150, 30, 30), items [8].amount.ToString());    
         GUI.Label (new Rect (93, 150, 30, 30), items [9].amount.ToString());
         GUI.Label (new Rect (153, 150, 30, 30), items [10].amount.ToString());
         GUI.Label (new Rect (213, 150, 30, 30), items [11].amount.ToString());
         GUI.Label (new Rect (33, 210, 30, 30), items [12].amount.ToString());
         GUI.Label (new Rect (93, 210, 30, 30), items [13].amount.ToString());
         GUI.Label (new Rect (153, 210, 30, 30), items [14].amount.ToString());
         GUI.Label (new Rect (213, 210, 30, 30), items [15].amount.ToString());
         GUI.Label (new Rect (33, 270, 30, 30), items [16].amount.ToString());
         GUI.Label (new Rect (93, 270, 30, 30), items [17].amount.ToString());
         GUI.Label (new Rect (153, 270, 30, 30), items [18].amount.ToString());
         GUI.Label (new Rect (213, 270, 30, 30), items [19].amount.ToString());
 
 
         GUI.EndGroup ();
         GUI.DragWindow (new Rect (0, 0, 10000, 60));
     }
 
     void OnTriggerEnter(Collider item){
         if (item.gameObject.tag == "BasicSword") {
             LookForEmptySlot (itemsToUse [0]);
         } else if (item.gameObject.tag == "AirTanks") {
             LookForEmptySlot (itemsToUse [1]);
         } else if (item.gameObject.tag == "CastleHelmet") {
             LookForEmptySlot(itemsToUse[2]);
         }
     }
 
     int LookForEmptySlot(Item itemToAdd){
         for (int i = 0; i < 20; i ++) {
             if (items [i].itemName == itemToAdd.itemName) {
                 items [i].amount ++;
                 return i;
             }
         }
         for(int i = 0; i < 20; i ++){
              if(items[i].empty == true){
                 items[i].itemName = itemToAdd.itemName;
                 items[i].itemDescription = itemToAdd.itemDescription;
                 items[i].amount = itemToAdd.amount;
                 items[i].physicalItem = itemToAdd.physicalItem;
                 items[i].itemIcon = itemToAdd.itemIcon;
                 items[i].rarity = itemToAdd.rarity;
                 items[i].itemType = itemToAdd.itemType;
                 return i;
             }
         }
         //There isn't an index of 21 so it won't be added
         return 21;
     }
 
 }
 
Comment
Add comment · Show 4
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 siaran · May 30, 2015 at 01:10 PM 2
Share

Ok, first of all, you need to use more loops. You have 20 labels and buttons that use almost exactly the same code. Don't write it 20 times. It also makes it a lot more readable & easier to debug.

avatar image Bincredible · May 30, 2015 at 06:04 PM 0
Share

Yeah but I thought a loop would only be good for Lists. If I use an array I can move the items around and keep track what's in each slot easier can't I?

avatar image siaran · May 30, 2015 at 06:07 PM 2
Share

There is no reason not to loop through an array...where did you get the idea loops are only good for lists?

avatar image YoungDeveloper · May 30, 2015 at 07:10 PM 0
Share

The code is awful, this is gc torture script.

Here's an approximate example of how it should be done. This will draw inventory in any size you specify using old ongui. No new object creation, everything is clean and beautiful.

 using UnityEngine;
 
 public class Example : $$anonymous$$onoBehaviour {
 
     
     private byte WIDTH = 3;
     private byte HEIGHT = 5;
     
     private byte render_h = 0;
     private byte render_w = 0;
     
     private Rect slot = new Rect(0,0,32,32);
 
     private void OnGUI(){    
         render_h = 0;
         slot.y = 0;
         
         for(; render_h < HEIGHT; render_h++){
             render_w = 0;
 
             for(; render_w < WIDTH; render_w++){
                 GUI.Box(slot, string.Empty);
                 
                 slot.x += slot.width;
             }
             slot.x = 0;
             slot.y += slot.height;
 
         }
     }
 }

$$anonymous$$ore about this code and my full answer you can check out in this post in forum where i hang out.

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

21 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

Related Questions

Can't Assign Item In Array 1 Answer

Scroll List Problem 1 Answer

Preventing Items from shifting in a list 1 Answer

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

Getting info from object within an array 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