Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 obnix · May 26, 2017 at 12:16 PM · 2d gamerpginventory systemtop-down

Add item to GUI inventory and drop him back in the scene with keys

Hello, I'm trying to make an inventory system for 2D top down game. I would like to pick up objects with a key and store it into my inventory. I should be able to see what items I have in my inventory and be able to drop them back in the scene with a key. I'm stack and have no idea what to do :confused:. Any help would be grateful. This is my code for the inventory:

 public class Inventory : MonoBehaviour {
  
     public GameObject[] inventory = new GameObject[10];
  
     public void addItem (GameObject item)
     {
         bool itemAdded = false;
         //find the first open slot in the inventory
         for (int i = 0; i < inventory.Length; i++)
         {
             if(inventory [i] == null)
             {
                 inventory[i] = item;
                 Debug.Log(item.name  + " " + "was added");
                 itemAdded = true;
                 break;
             }
         }
         //inventory full
         if (!itemAdded)
         {
             Debug.Log("Inventory full!");
         }
     }
  
     public GameObject FindItemByType(string itemType)
     {
         for (int i = 0; i< inventory.Length; i++)
         {
             if(inventory[i] != null)
             {
                 if(inventory[i].GetComponent<IntercationObject> ().itemType == itemType)
                 {
                     //we found the type of item we wanted
                     return inventory[i];
                 }
             }
         }
         //item not found
         return null;
     }
  
     public void removeItem (GameObject item)
     {
         for(int i = 0; i<inventory.Length; i++)
         {
             if(inventory[i] == item)
                 //remove founded item
             {
                 inventory[i] = null;
                 Debug.Log(item.name + "was removed from the inventory");
                 break;
  
             }
         }
     }
 }


This is my code for the interaction with the object:

 public class IntercationObject : MonoBehaviour {
  
     public bool inventory; // if true this item can be stored
     public string itemType; //this will tell what type of object it is
  
     public void DoInteraction()
  
     {
         //Picked and put into inventory
         gameObject.SetActive(false);
     }
 }

This is my code for the interaction with the player:

 public class PlayerInteraction : MonoBehaviour
 {
  
     public GameObject currentInterObj = null;
     public IntercationObject currentInterObjScript = null;
     public Inventory inventory;
  
      void Update()
     {
         if (Input.GetButtonDown("Interact") && currentInterObj)
         {
             if (currentInterObjScript.inventory)
             {
                 inventory.addItem(currentInterObj);
             }
             // Do something with the object
             currentInterObj.SendMessage("DoInteraction");
         }
  
         //Use item from inventory
         if (Input.GetButtonDown("Use item"))
         {
             //Check the inventory for an item
             GameObject Kost = inventory.FindItemByType("Bone");
             Debug.Log("nešto");
          
         }
  
     }
  
  
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag("interObject"))
         {
             Debug.Log(other.name);
             currentInterObj = other.gameObject;
             currentInterObjScript = currentInterObj.GetComponent<IntercationObject>();
         }
     }
     private void OnTriggerExit2D(Collider2D other)
     {
         if (other.CompareTag("interObject"))
         {
             if (other.gameObject == currentInterObj)
             {
                 currentInterObj = null;
             }
  
         }
     }
 }

And this is my code for the GUI inventory:

 public class GUIInventory : MonoBehaviour {
     private bool inventoryWindowToggle = false;
     private Rect inventoryWindowRect = new Rect(300, 100, 400, 400);
    
  
     // Use this for initialization
     void Start () {
        
     }
    
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown("ShowHideInventory"))
         {
             inventoryWindowToggle = !inventoryWindowToggle;
         }
        
     }
  
      void OnGUI()
     {
         inventoryWindowToggle = GUI.Toggle(new Rect(0, 0, 0, 0), inventoryWindowToggle, "inventory");
         if (inventoryWindowToggle)
         {
             inventoryWindowRect = GUI.Window(0, inventoryWindowRect, inventoryWindowMethod, "inventory");
         }
     }
  
     void inventoryWindowMethod(int WindowId)
     {
         GUILayout.BeginHorizontal();
         GUILayout.Button("Item 1", GUILayout.Height(50));
         GUILayout.Button("Item 2", GUILayout.Height(50));
         GUILayout.Button("Item 3", GUILayout.Height(50));
         GUILayout.EndHorizontal();
  
         GUILayout.BeginHorizontal();
         GUILayout.Button("Item 4", GUILayout.Height(50));
         GUILayout.Button("Item 5", GUILayout.Height(50));
         GUILayout.Button("Item 6", GUILayout.Height(50));
         GUILayout.EndHorizontal();
  
         GUILayout.BeginHorizontal();
         GUILayout.Button("Item 7", GUILayout.Height(50));
         GUILayout.Button("Item 8", GUILayout.Height(50));
         GUILayout.Button("Item 9", GUILayout.Height(50));
         GUILayout.EndHorizontal();
  
         GUILayout.BeginHorizontal();
         GUILayout.Button("Item 10", GUILayout.Height(50));
         GUILayout.EndHorizontal();
  
     }
  
 }




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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ismaelnascimento01 · May 26, 2017 at 12:45 PM

do you with UI your inventory ?

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

8-directional orientation, top down 2D, seperate from movement 0 Answers

AI script that follows player in a distance 1 Answer

In Unity 2d is it possible to change which sprite sheet a player is using? 0 Answers

Tilemap Border Size 2 Answers

How can I make walls with Collider2D components for a top-down game in Unity 2018? 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