- Home /
Collect pickup if mouse button is not pressed
I guys, this is my first question and I'm a newbie in game development. I'm making my first game, you throw a ball to collect pickups. Everything is working fine, but I'd like to prevent the collection of the pickup if the mouse button is pressed. I wrote this code to detect collision between the ball and the pickup, and it works:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Collectible"))
{
other.gameObject.SetActive(false);
}
}
To try to prevent the collection of the pickup when the mouse button is pressed i created a bool variable "canCollect", then in OnMouseDown i set it to false, then set it to true again in OnMouseUp. I made this variable public to check the value of it while trying the game in unity and it works, when the left mouse button is pressed canCollect = false when the left mouse button is releasead canCollect = true. So I modified the code like this:
void OnTriggerEnter2D(Collider2D other)
{
if (canCollect)
{
if (other.gameObject.CompareTag("Collectible"))
{
other.gameObject.SetActive(false);
}
}
}
But it doesn't work, the pickup is collected everytime, even if the lmb is pressed... What am I doing wrong?
Thanks in advance for any suggestion!