Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 ZaneBrown · Jan 26 at 01:24 PM · buglistsfirst-person-controllerinventory system

Issue with equipping object

When picking up an object, they are automatically added to the inventory. I can correctly add the item to my hand when I press "1". However, when I press "2" the second item stacks on top of the first item.

However, when I press "1" again, the second item disappears correctly and when I press "2" again, the first item disappears correctly.

The problem is that the first item will not disappear correctly when I press "2" for the first time. It only works correctly when I press "1" twice.

What exactly is the issue here? I've tried deactivating the object when another button is pressed, but I get the same result. Here's some of the code I am talking about.

Select Items to be Equipped

 void SelectItems()
     {
         foreach (GameObject item in items)
         {
             if (Input.GetKeyDown(KeyCode.Alpha1) && items.Count >= 1)
             {
                 items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
                 itemToBeEquipped = 0;
                 items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
                 Debug.Log("Item 1 Equipped");
                 
             }
             else if (Input.GetKeyDown(KeyCode.Alpha2) && items.Count >= 2)
             {
                 items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
                 itemToBeEquipped = 1;
                 items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
                 Debug.Log("Item 2 Equipped");
             }
             else if (Input.GetKeyDown(KeyCode.Alpha3) && items.Count >= 3)
             {
                 items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
                 itemToBeEquipped = 2;
                 items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
                 Debug.Log("Item 3 Equipped");
             }
             else if (Input.GetKeyDown(KeyCode.Alpha4) && items.Count >= 4)
             {
                 items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
                 itemToBeEquipped = 3;
                 items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
                 Debug.Log("Item 4 Equipped");
             }
             else if (Input.GetKeyDown(KeyCode.Alpha5) && items.Count == 5)
             {
                 items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
                 itemToBeEquipped = 4;
                 items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
                 Debug.Log("Item 5 Equipped");
             }
         }
     }


PickUpItem Method

 public void PickUpItem()
     {
         if (InventoryScript.items.Count < 5)
         {
             //disable picked up item colliders and rigidbody
             coll1.enabled = false;
             coll2.enabled = false;
             rb.isKinematic = true;
 
             gameObject.transform.parent = pos_pickedUpItem.transform;
             gameObject.transform.position = pos_pickedUpItem.position;
             isInInventory = true;
             isEquipped = true;
             InventoryScript.itemToBeEquipped = InventoryScript.items.Count;
             InventoryScript.items.Add(gameObject);
             EquipItem();
             gameObject.GetComponent<MeshRenderer>().enabled = false;
 
             //Debug.Log("Picked up " + str_ItemName + "!");
             //Debug.Log("There are " + InventoryScript.items.Count + " in your inventory");
         }
         else if (InventoryScript.items.Count == 5)
         {
             Debug.Log("Error: Not enough space to pick up this item!");
         }
     }

Equip / Unequip Method

 public void EquipItem()
     {
         gameObject.GetComponent<MeshRenderer>().enabled = true;
         isEquipped = true;
         isActivated = true;
         //gameObject.SetActive(true);
 
         //Debug.Log("Equipped " + str_ItemName + "!");
     }
     public void UnequipItem()
     {
         gameObject.GetComponent<MeshRenderer>().enabled = false;
         isEquipped = false;
         isActivated = false;
         //gameObject.SetActive(false);
 
         //Debug.Log("Unequipped " + str_ItemName + "!");
     }

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

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

160 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 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 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 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 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 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 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

How to get the current items in my inventory with respective stock of the items 1 Answer

Weird lighting bugs 1 Answer

Adding object to list add the same object to another list 1 Answer

Add to element quantity value if it already exists 0 Answers

List values change for no apparent reason 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