# How to know an object is in desired position?

So, I’m trying to learn C# by making a 2D game including two grids of ‘blocks’ all x/y=1 apart from each other. Both grids are identical and separated and act as a ‘side’ for each player. Each of these blocks is spawned in at the beginning of the game as a prefab by this:

``````float blockNumber;
float column;
float row;
static Vector2 blockSpotOne;
static Vector2 blockSpotTwo;

void Start ()
{
CreateBlocks();
}
void CreateBlocks()
{
while (blockNumber < 138)
{
blockSpotOne.y = 4.485f - row;
blockSpotOne.x = -7.48f + column;
blockSpotTwo.y = 4.485f - row;
blockSpotTwo.x = 1.52f + column;
Instantiate(Block, blockSpotOne, Block.transform.rotation = Quaternion.Euler(0, 0, 0));
Instantiate(Block, blockSpotTwo, Block.transform.rotation = Quaternion.Euler(0, 0, 0));
blockNumber += 1;
column += 1;
if (column == 7)
{
column = 0;
row += 1;
}
}
}
``````

The players each control their own GameObject shown by a big red X that moves by 1 everytime WASD or arrowkeys are hit. I’m sure there are better ways to do what I’m doing, and I’d be happy to hear them, but I’d like to know how to have the block check if the X is on its location, and if so, be destroyed/deactivated by pushing a button, by checking if the X is in the same location or if it is close enough using Vector.Distance. In this situation ‘Target’ refers to the X. (The following script is attached to every block prefab)

``````static Vector2 targetOnePosition;
static Vector2 targetTwoPosition;
static Vector2 currentPosition;
public GameObject TargetOne;
public GameObject TargetTwo;

void Start()
{
currentPosition.y = transform.position.y;
currentPosition.x = transform.position.x;
BreakBlockOne();
BreakBlockTwo();
}
void Update()
{
TargetOne.transform.position = targetOnePosition;
TargetTwo.transform.position = targetTwoPosition;
BreakBlockOne();
BreakBlockTwo();
}
void BreakBlockOne()
{
if (Input.GetKeyDown(KeyCode.E) && Vector2.Distance(targetOnePosition,currentPosition) < 1)
{
gameObject.SetActive(false);
}
}

void BreakBlockTwo()
{
if (Input.GetKeyDown(KeyCode.Comma) && targetTwoPosition == currentPosition)
{
Destroy(gameObject);
}
}
``````

BreakBlockOne and BreakBlockTwo are a couple of several ways I’ve tried this, and it’s driving me crazy having no clue what’s wrong. Once again, any other advice is appreciated.

You could put a collider on your block objects and your X object, and set IsTrigger to true for all of them. Give the blocks a “Block” tag and the X a “Cross” tag (or however you want to name them). Then in your block script put something like this:

``````private bool _isCrossOnMe;

public void OnTriggerEnter(Collider other)
{
//Ignore any collider that isn't a cross
if (!other.CompareTag("Cross")) return;
_isCrossOnMe = true;
}

public void OnTriggerExit(Collider other)
{
//Ignore any collider that isn't a cross
if (!other.CompareTag("Cross")) return;
_isCrossOnMe = false;
}
``````

I hope I understood your post correctly, let me know if there is anything more you need help with.