How to see if two objects are touching each other

So, I am trying to check if two objects are touching each other, and this is the part of the script that I cant get to work

	public static bool IsTouchingS (BoxCollider2D.StraightS, BoxCollider2D.PlayerCar);

	void GetLocator ()
	{
		if (IsTouchingS == true)

There is already a function for this inside the Collider2D class:

public Collider2D objectCollider;
public Collider2D anotherCollider;

private void SomeMethod()
{
    if (objectCollider.IsTouching(anotherCollider))
    {
        // Do something;
    }
}

Well you could do one of two things. You could find the distance between the objects and decide if they’re touching based off that. Obviously this doesn’t actually tell you if they’re touching but it’s one method.
You could also use your box colliders and listen for collisions.

Try OnCollisionStay2D for checking for two object touching each other, more info and example code here:

Hello, This post was semi solved a while ago, but it didn’t work for me so I figured I’d post the solution I came up with and explain why this solution didn’t work for me.

This solution didn’t work for me because my object is 3d and can be touching two objects at the same time. Most other posts try to get by this by verifying the touching object with a tag. However I had the possibility of an object touching two items with the same tag at the same time. For example, I needed my player to be able to jump. However I only want them to be able to jump if they were touching the environment. The player could both be touching the ground and some stairs at the same time. If the player was touching the side of stairs while standing on the ground, then when the player stopped touching the stairs, they would be unable to jump. However they would still be touching the ground, so logically they should be able to jump. Heres my solution to this problem:

First the basics:

    private bool canJump = false;
    FixedUpdate(){
    if (canJump)
            {
                if (Input.GetKey(KeyCode.Space))
                {
                    Rigidbody.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
                   
                }
            }
    }

This makes it so the player will jump when the bool is true, and by default the bool is false. I have the player start above the map and fall into it so that they start with the onCollision I’m about to write:

Start by adding to your variable list:

      private GameObject[] touchingObjects;

Then add to your Start():

private void Start()
    {
        touchingObjects = new GameObject[10];
     }

Finally the OnCollision Enter and Exit:

 private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Environment")
        {
            int tempint = touchingObjects.Length;
            GameObject[] tempArray = new GameObject[tempint+1];
            for(int i = 0; i< tempint; i++)
            {
                tempArray _= touchingObjects*;*_

}
tempArray[tempint] = collision.gameObject;
touchingObjects = tempArray;
canJump = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == “Environment”)
{
canJump = false;
System.Collections.Generic.List list = new System.Collections.Generic.List(touchingObjects);
list.Remove(collision.gameObject);
touchingObjects = list.ToArray();
for(int i = 0; i < touchingObjects.Length; i++)
{
if (touchingObjects*)*
{
if (touchingObjects*.tag == “Environment”)*
{
canJump = true;
}
}
}
}
}
This way, all objects that come into contact with the player object are added to an array. When the object is no longer touching, they are removed from the array. When they are removed, the player will no longer be able to jump unless one of the objects they are still touching also has the “Environment” tag.