- Home /
Searching your inventory for an item,How do i search for an item in my inventory?
i have a functioning inventory system that allows me to pickup and drop items but i cannot, for the life of me, figure out how to check if my inventory includes a specific item such as a key so that a block can be disabled.
code for my inventory:
public class Inventory : MonoBehaviour {
public bool[] isFull;
public GameObject[] slots;
}
Code for the slots in my inventory
public class Slot : MonoBehaviour {
private Inventory inventory;
public int i;
private void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
private void Update()
{
if(transform.childCount <= 0)
{
inventory.isFull[i] = false;
}
}
public void DropItem()
{
foreach(Transform child in transform)
{
GameObject.Destroy(child.gameObject);
}
}
}
code for picking up items:
public class Pickup : MonoBehaviour { private Inventory inventory; public GameObject itemButton;
private void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>(); //sets inventory equal to the inventory component attached to our player
}
void OnTriggerEnter2D(Collider2D other) { //checks when character collides with pickup, indicating that you can add it to your inventory
if (other.CompareTag("Player")) { //checks if what the object collided with was our player. if it is.. then add it to the inventory
for(int i = 0; i < inventory.slots.Length; i++) //To do this we have to check if the inventory has empty slots or if its full.
{
if(inventory.isFull[i] == false) //if empty, add it to the inventory
{
//item can be added to inventory
inventory.isFull[i] = true;
Instantiate(itemButton, inventory.slots[i].transform, false); //instantiates item button at first inventory slot that isn't already full. Parents buttun to inventory slot of i. false bc theres no z
Destroy(gameObject); //makes object disappear on screen after you have put it in your inventory.
break;
}
}
}
}
}
my current nonworking code to check my inventory: public class door : MonoBehaviour { public GameObject myobject; public bool activateme; private Inventory inventory;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
for(int i=0; i<8; i++)
if ( inventory.slots[i] == GameObject.FindGameObjectWithTag("gold key"))
if (activateme == true)
{
myobject.SetActive(false);
}
}
}
its supposed to work like so: the player approaches the door with a 2d collider on it. if he has the key, he will (based on prompts in the game) know to press the space bar so that he can get through. my code should then search the inventory array to see if he has the key. if he does, disable the 2d door and allow him to pass. this is the part that isn't working: if ( inventory.slots[i] == GameObject.FindGameObjectWithTag("gold key"))
it always comes out to be false.
Answer by zactanpeterson · Nov 25, 2019 at 10:40 PM
hard to tell what exactly is going on in your inventory but the basics you should have is a slot class that houses an id and an amount for an item, and an inventory that houses an array of these slots, then you should be able to have a for loop to go through those slots and check the id of the slot to see if you have the item.
it looks like you loop through the slots (which is just a gameObject) and check to see if it equals a gameObject with the tag "gold key", instead you should do a check like:
inventory.slots[i].id == (id of gold key)
Your answer
Follow this Question
Related Questions
adding inventory items is half-working 0 Answers
How can I delete an item from this inventory system? 1 Answer
unity3d simple inventory 1 Answer
[C#] Grouping variables 1 Answer
Inventory AddItem help 1 Answer