[C#] Check position of multiple GameObjects with mouse click position

Hey guys,

I’m really new to programming and I’ve been bashing my head against the wall with this. I’ve searched for existing questions/answers but seeing as I don’t fully understand this, they’re of very little help as I get errors I have no idea how to fix.

The goal: (Imagine a Plants vs Zombies style game) When placing a defender, first check to see if there’s already a defender in that square. If not, place a defender. If so, communicate that it’s not allowed.

My question: How do I store the positions of the objects in the placedDefenders array and then check them against the position of the mouse click (roundedPos)?

I have the GameObject array working properly, but then figuring out how to get the positions of those objects is where I come undone (let alone checking them against the mouse click position).

Here’s my script, I’ve deleted all the crud where I was trying to store the positions in another array because it was getting very sloppy. I hope it makes sense.

I appreciate any help.

using UnityEngine;
using System.Collections;

public class DefenderSpawner : MonoBehaviour {

	Camera myCamera;
	GameObject defenderParent;

	void Start () {
		myCamera = Camera.main;
		defenderParent = GameObject.Find ("Defenders");
		
		if (defenderParent == null) {
			defenderParent = new GameObject ("Defenders");
		}
	}

	void OnMouseDown () {
		// Coordinate stuff
		Vector2 rawPos = CalculateWorldPointOfMouseClick ();
		Vector2 roundedPos = SnapToGrid (rawPos);

		// Arrays for placement checking
		Vector3[] placedDefenderPositions;
		GameObject[] placedDefenders;

		placedDefenders = GameObject.FindGameObjectsWithTag ("Defender");

		// Instantiate Defender and parent it in hierarchy
		GameObject defender = Instantiate (Button.selectedDefender, roundedPos, Quaternion.identity) as GameObject;
		defender.transform.parent = defenderParent.transform;
	}

	Vector2 SnapToGrid (Vector2 rawWorldPos) {
		float newX = Mathf.RoundToInt (rawWorldPos.x);
		float newY = Mathf.RoundToInt (rawWorldPos.y);

		return new Vector2 (newX, newY);
	}

	Vector2 CalculateWorldPointOfMouseClick () {
		float mouseX = Input.mousePosition.x;
		float mouseY = Input.mousePosition.y;
		float distanceFromCamera = 10f;

		Vector3 cameraPoints = new Vector3 (mouseX, mouseY, distanceFromCamera);
		Vector2 worldPos = myCamera.ScreenToWorldPoint (cameraPoints);

		return worldPos;
	}
}

You can get the position of any gameObject in Unity by calling theGameObject.transform.position. This gives you the world-space position of the object. You could check them against the mouse position by doing a for loop like so:

for (int i=0;i<placedDefenders.Length;i++)
{
     Vector3 objectPosition = placedDefenders*.transform.position;*

Vector2 objectPosition2D = new Vector2(objectPosition.x, objectPosition.y);

if (SnapToGrid(objectPosition2D) == roundedPos)
{
//One of the defenders is in this spot!
NotifyPlayerFunction();
break;
}
}