How to detect specific collisions for items.
I am attempting to create an item mechanic for a platformer I am working on. However, it does not seem to be working. The goal is to pick up an item (a square with a box collider 2D set to trigger) , and then to place it in a receptacle (another square set to trigger). I have it so that when you overlap the receptacle and have the item picked up (global variable) it says in the console "Item Detected!" The issue is that it says this even when you do not have the item picked up, it says "Item Detected!" How would i fix this? I believe it is that when you overlap the receptacle it runs the code for the item and sets Item.itemPickup to true and then runs the receptacle script.
Anyways, here is my code: Item itself: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Item : MonoBehaviour
{
public Collider2D Collider; // i have tried setting this to both the player and the item's hitbox
public GameObject SolarPanel;
static public bool itemPickup;
private void OnTriggerEnter2D(Collider2D Collider)
{
Debug.Log("Pickup!");
SolarPanel.SetActive(false);
itemPickup = true;
}
}
and the Receptacle:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPlacement : MonoBehaviour
{
public Collider2D Collider; // i have tried setting this to both the player and the item's hitbox
private void OnTriggerEnter2D(Collider2D Collider)
{
if(Item.itemPickup = true) {
Debug.Log("Item Detected!");
Item.itemPickup = false;
// sprite change
}
}
}
Thank you for your time.