Making a UI-button's OnClick() check for 2 arbitrary objects colliding

I currently have a button, that I want to be able to press when a game object (containing a Collider2D and RigidBody2D) collides with another object (only a Collider2D component on). So I’m thinking I set two public GameObjects in my script, which are these two objects. But on the OnClick() method in the Button that has the script attached, I’m not sure how to write the code.

public class CorrectAnswer : MonoBehaviour {

public GameObject collider,arrow; 
...
... 

void OnClick(){
if(arrow.collider2D.. ?)
  //do some stuff here
}

I want the behaviour to be like the

OnTriggerStay2D(Collider other)

method, where I have a boolean set to true which means the button should do something when pressed…

Regards

The simplest way would be to let the Unity physics system handle it. Just turn the collision detection function on then wait for the next fixed update cycle to check for collisions.

If this does not work you could always try OverlapSphere or Sweep. These are both rather limited in their application.

What I did is make a public static boolean that is set to true when the objects collide. Then from the OnClick list in the components view of Unity (of the button) I added the script with a call to the method (Click())that checks if the boolean is true. If it is, then do stuff.

Works fine :slight_smile: