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 NutellaDaddy · Mar 29, 2014 at 12:02 AM · c#listinventoryremove

How can I remove an item ffrom a list.

I know that I can remove it with listName.Remove(blah blah) and that would work fine, but I want to remove an item from the button I am holding it in. I have items in a chest and when you click them they go into your inventory, but they also stay in the chest. My problem is that when you click a button to remove that item it just removes the last one in the list and not the one that you click on. Does anybody know the solution to this? If you need to see the script I can post it ,but otherwise please help me find a solution. using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class OpenCrateScript : MonoBehaviour 
 {    
     static public bool lootCrateOpen = false;    //If the loot crate is open or not    
     private float maxDistance;                    //Max distance the player can be from the chest to open it.
     public Transform player;                    //The player opening the chest
     public GameObject crate;                    //The crate that we want to delete or spawn at certain times.
 
     public Texture2D CrateWindow;
     public Texture2D CloseButton;
     public GUIStyle CrateSlotStyle;
     public GUIStyle CrateButtons;
     public GUIStyle CrateScrollBar;
     public GUIStyle Text;
     public GUIStyle Empty1;
     
     public static List<Item> lootItems;
     private int buttonWidth = 66;
     private int buttonHeight = 66;
     private int offsetX = 217;
     private int offsetY = 62;
     private int maxCols = 3;
 
     //LOOT WINDOW SCROLL VIEW
     private Vector2 crateWindowScrollView= Vector2.zero;
     void Start () 
     {
         maxDistance = 2.5f;
         lootItems = new List<Item> ();
         PopulateCrate ();
     }
     void Update ()
     {
         if (craftingMenu.craftingMenuOn == true || BuildingMenu.buildingMenuOn == true) 
         {
             lootCrateOpen = false;
         }
         if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == false) {
             RaycastHit hit;
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
                 if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, player.transform.position) <= maxDistance) {
                     lootCrateOpen = true;
                     inventorySystem.inventoryOn = true;
                     inventorySystem.toolbarOn = true;
                 }
             }
             if ( lootCrateOpen == true && Input.GetKeyUp(KeyCode.Tab))
             {
                 inventorySystem.inventoryOn = false;
                 inventorySystem.toolbarOn = false;
                 lootCrateOpen = false;
             }
         }
         if (inventorySystem.inventoryOn == true && Input.GetKeyUp (KeyCode.Tab)) 
         {
             lootCrateOpen = false;
         }
 
     }
     void OnGUI()
     {
         if (lootCrateOpen == true) 
         {
             GUI.BeginGroup(new Rect(701,31,690,800), CrateWindow);
                 inventorySystem.inventoryOn = true;
                 inventorySystem.toolbarOn = true;
                 GUI.Label(new Rect(220,25,100,20),"Small Crate",Text);
                     if(GUI.Button(new Rect(430,25,15,15),CloseButton,Text))
                     {
                         lootCrateOpen = false;
                         inventorySystem.inventoryOn = false;
                         inventorySystem.toolbarOn = false;
                     }
                 crateWindowScrollView = GUI.BeginScrollView(new Rect(-5,55,450,285), crateWindowScrollView,new Rect(-15,0,42,700));
                     for(int cnt = 0; cnt < 18; cnt++){
                         if(cnt < lootItems.Count)
                             {
                                 if(GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle))
                                     {
                                         AddItem(cnt);
                                     }
                             }
                         else
                             {
                                 GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
                             }
                     }
                     GUI.EndScrollView();
 
             GUI.EndGroup();
         }
     }
     private void PopulateCrate()
     {
         for (int cnt = 0; cnt < 5; cnt++) 
         {
             lootItems.Add (new Item());
         }
     }
     private void AddItem(int cnt)
     {
         inventorySystem.inventory.Add (lootItems [cnt]);
         lootItems.RemoveAt (cnt);
     }
 }
 
Comment
Add comment · Show 5
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 getyour411 · Mar 29, 2014 at 12:15 AM 0
Share

I think I had a similar situation. What you need to know is the list indexID of the item (you might have a bagPosition or chestPosition which doesn't correspond ot the list indexID).

If that's sounds about right take a look at this http://answers.unity3d.com/questions/629494/c-lists-inventory-logic.html

starting at line 17, the LINQ code to get the list indexID

avatar image Mrslayer01 · Mar 29, 2014 at 12:16 AM 0
Share

if you wouldn't $$anonymous$$d to post your code I could take a look at it.

avatar image ArkaneX · Mar 29, 2014 at 12:48 AM 1
Share

Have you tried the solution I posted under answer to your previous question?

avatar image NutellaDaddy · Mar 29, 2014 at 01:04 AM 0
Share

Yes and it doesn't work. It still only removes the last one.

avatar image NutellaDaddy · Mar 29, 2014 at 01:10 AM 0
Share

also getyour411 I was confused by the index thing. Could you please explain how I could use it? Here I'll post up the script I'm using

1 Reply

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

Answer by zharik86 · Mar 29, 2014 at 07:37 AM

The code seemingly correct, and deleting comes from itemloot truly. But the output to the screen, that that remained, in my opinion, incorrectly. After all you have a cycle from 0 to 17. You define that thus there can be a cell as loot from the list (button), or it isn't present (label). When you delete any cell from the list, the quantity of its elements decreases on 1. But the output to the screen will be as though you deleted the last element. You look, you have initially 17 elements and all of them on the screen as buttons. You delete any of them, then you still have 16 elements, but the cycle goes again from 0 to 17 therefore from your code only the last element will be as label. That to correct it, you need to explain to the system, what element was remote, for example:

  for(int cnt = 0; cnt < 18; cnt++) {
   if(cnt < lootItems.Count) {
    if (lootItems[cnt] != null) { // my explain for system of loot
     if(GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle)) {
      AddItem(cnt);
     }
    } else {
     GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
    }
   }
  }

And your AddItem() function:

  private void AddItem(int cnt) {
   inventorySystem.inventory.Add (lootItems [cnt]);
   //lootItems.RemoveAt (cnt);
   lootItems[cnt] = null;
  }

I hope it to you will help.

Comment
Add comment · Show 2 · 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 NutellaDaddy · Mar 29, 2014 at 05:21 PM 0
Share

The only problem is that the other slots that should be in the chest and show up ad empty aren't there. How could I bring them back to where there are all 18 of them ,but only the 5 have items in it?

avatar image zharik86 · Mar 29, 2014 at 07:35 PM 0
Share

@NutellaDaddy If I correctly understood, you want in addition to relocation in inventory, to make relocation back of inventory. Then it is necessary to move to the first free slot. For example:

  public void relocation(int numIt) {
   bool addingList = true; //Just in case, and that suddenly you will exceed quantity of cells of the list loot
   for(int i=0; i <lootItems.Count; i++) {
    if (lootItems[i] == null) {
     addingList = false;
     lootItems[i] = ...//item, from your inventory behind number numIt
     break;
    }
   }
   if(addingItem) {
    lootItems.Add(...) //item, from inventory behind number numIt
   }
  }

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

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

Removing objects from a list in C# 2 Answers

Add random amount of random items from one list to another? 2 Answers

Problem with removing an item from a list 0 Answers

Unknown remaining objects after removing and destroying from list and parent game objects 0 Answers

How to ignore base class? 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