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 *= 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(); //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 == false) //if empty, add it to the inventory
{
//item can be added to inventory
inventory.isFull = true;
Instantiate(itemButton, inventory.slots*.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 == 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 == GameObject.FindGameObjectWithTag(“gold key”))
it always comes out to be false.