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 . 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();
}
}