How to tell if two objects are right next to each other in a 2d game?

I have two different blocks, one called Resource and one called Extractor. How do I tell if they are right next to each other on the x axis? Here is the code I tried:

#pragma strict
var Extractor : GameObject;
var Resource : GameObject;
 
function Update () {
if(Extractor.transform.position.x == Resource.transform.position.x + 1 || Extractor.transform.position.x == Resource.transform.position.x - 1 ){
    Debug.Log("Extract");
}
}

Simply check the distance between them using…

if(Mathf.Abs(Extractor.transform.position.x - Resource.transform.position.x)< distanceYouExpect ){
Debug.Log(“Next to each other…”);
}

or you can try this also… to make sure in all axes

if(Vector3.distance(Extractor.transform.position,Resource.transform.position).magnitude < distanceyouExpect))
{
Debug.Log(“Next to each other…”);
}

Your original code should work if you just change the “==” to the appropriate “<=” or “>=”

Like this:

if(Extractor.transform.position.x <= Resource.transform.position.x + 1 || Extractor.transform.position.x >= Resource.transform.position.x - 1 ){