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 Sauskue · Jun 24, 2013 at 12:33 PM · listinventoryrpgitem

Problem with dropping items from my inventory

I made a simple system for adding items into my inventory, but when I try to remove an object at a certain position, for some reason if it's not the first index in the list, it will transform the one above it into itself, it that makes any sense. This is for picking up items..

 static GameObject go;
     RaycastHit hit;
     public Camera myCamera;
     public bool canPickUpItem;
     public bool canLootChest;
     public Item loot;
     public List<GameObject> loots;
     
     void Update(){
         if(Input.GetKeyDown(KeyCode.E)){
             if(canPickUpItem){
                 Loot(loot);
             }
 void Loot(Item item){
         SendMessage("AddItem", item);
         item.SendMessage("Destroy");
     }
 void OnGUI(){
         if(Physics.Raycast(transform.position, myCamera.transform.forward, out hit, pickUpRange)){
             go = hit.collider.gameObject;
             if(go.tag == "Item"){
                 GUI.TextArea(new Rect(Screen.width/2, Screen.height/2, 150, 40), "Press 'E' to pick up '" + go.name + "'");
                 loot = go.GetComponent<Item>();
                 canPickUpItem = true;
             }

..and this adds them to the inventory:

 public List<Item> playerInventory;
     Vector2 scrollView;
     public bool showInventory;
     Rect inventoryRect;
     static GameObject model;
     public GameObject objThrowSpawn;
 void Start(){
         playerInventory = new List<Item>();
 }
 void AddItem(Item it){
         playerInventory.Add(it);
         Debug.Log("Picked up '" + it.name + "'");
     }
     
     void RemoveItem(Item it){
         Debug.Log(playerInventory.IndexOf(it));
         playerInventory.Remove(it);
     }
 void OnGUI(){
 for(int x = 0; x < playerInventory.Count; x++){
             Item index = playerInventory[x];
             GUILayout.BeginVertical();
             GUILayout.BeginHorizontal();
             GUILayout.Label(index.name);
             if(index.type == Type.weapon){
                 if(GUILayout.Button("Equip Left")){
                     Item curIndex = playerInventory[x];
                     GameObject equipLeft = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.leftEquip.position, curIndex.leftEquip.rotation);
                     animation.CrossFade(curIndex.name + "EquipLeft");
                 }
                 if(GUILayout.Button("Equip Right")){
                     Item curIndex = playerInventory[x];
                     GameObject equipRight = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.rightEquip.position, curIndex.rightEquip.rotation);
                     animation.CrossFade(curIndex.name + "EquipRight");
                 }
             }
             else if(index.type == Type.armor){
                 Item curIndex = playerInventory[x];
                 if(GUILayout.Button("Equip")){
                     GameObject equipArmor = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.armorEquip.position, curIndex.armorEquip.rotation); 
                     animation.CrossFade(curIndex.name + "Equip");
                 }
             }
             if(GUILayout.Button("Drop")){
                 Item curIndex = playerInventory[x];
                 model = (GameObject)Instantiate(Resources.Load(curIndex.name), objThrowSpawn.transform.position, objThrowSpawn.transform.rotation);
                 model.name = Resources.Load(curIndex.name).name;
                 model.rigidbody.AddRelativeForce(new Vector3(0, 30, 60));
                 RemoveItem(curIndex);
             }
             else{
                 GUILayout.EndHorizontal();
                 GUILayout.Label(index.description);
                 GUILayout.EndVertical();
             }
 }

Sorry I know this is a lot of code but I don't know where I went wrong. Please help if you can, thank you.

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 SubatomicHero · Jun 26, 2013 at 09:17 AM 0
Share

is it this block of code causing the issue?

 if(GUILayout.Button("Drop")){
     Item curIndex = playerInventory[x];
     model = (GameObject)Instantiate(Resources.Load(curIndex.name), objThrowSpawn.transform.position, objThrowSpawn.transform.rotation);
     model.name = Resources.Load(curIndex.name).name;
     model.rigidbody.AddRelativeForce(new Vector3(0, 30, 60));
     RemoveItem(curIndex);
 }
avatar image Benproductions1 · Jun 26, 2013 at 11:24 AM 0
Share

I changed it to a private question. There is no reason for anyone to be able to edit it, so please don't mark it as a community question :)

avatar image amphoterik · Jun 26, 2013 at 11:48 AM 0
Share

Your menu system seems to be the culprit (I think?). It looks like you are draw a TON of buttons to the screen. Can we get a screenshot of the inventory system? $$anonymous$$y assumption is that there are multiple "Remove" buttons on top of each other, with the 1 position always at the top of that stack. Therefore, when you click it, it removes the first item. This works if the item you want is the first item.

Of course, this is an assumption until I can see it visually.

Also, let me know if I am misunderstanding your question.

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

18 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

Related Questions

[SOLVED] Inventory Stacking Problem [C#] 2 Answers

Scroll List Problem 1 Answer

Preventing Items from shifting in a list 1 Answer

How can I store game data outside of scripts.(e.g Total list of inventory items) 2 Answers

Can't Assign Item In 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