C# Trying to instantiate in location only once

I have a c# script where the user clicks and drags to instantiate an square every 4 units. I have that working, but the thing I have a problem with is trying to get the squares to only be able to instantiate at a location only once.

I am using a ray that is from the mouse position, and a RaycastHit that takes the location that hits the terrain (I want to make like a matrix of cubes that are on the ground, only on things with the tag “Terrain”)

In order to check I am assigning the position of the instantiated object to a Vector3 array with a size of 100. I have used many different strategies to check if the array is already there… I’ll put a few different strategies I tried and the result of them.

This works, but has multiple cubes per loc

if (Input.GetMouseButton(0)) {
			
	ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	if(Physics.Raycast(ray, out hit,150)  hit.collider.tag == "Terrain") {


                Instantiate(prefab, new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4), Quaternion.Euler(90,0,0));
		grid[gridNum] = new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4);
		gridNum++;
}
}

These are the different strategies I’ve tried that would be inside the second if statement

foreach (Vector3 x in grid) {
            			if (x.Equals (new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4))) {
               				Debug.Log ("YoNig");
            			} else {
						
							Instantiate(prefab, new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4), Quaternion.Euler(90,0,0));
							grid[gridNum] = new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4);
							gridNum++;
						}
					}

and this

for (int i = 0; i <= 100; i++) {
					
					if(grid[i].Equals(new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4))) {
						
						Debug.Log ("YoNig");
							
					} else {
						
						Instantiate(prefab, new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4), Quaternion.Euler(90,0,0));
						grid[gridNum] = new Vector3(Mathf.Round(hit.point.x/4)*4, hit.point.y, Mathf.Round(hit.point.z/4)*4);
					
						Debug.Log(grid[gridNum].x);
						Debug.Log ("wasn't there");
						gridNum++;
						
					}
					
				}

It’s kind of getting irritating and I’ve been trying for a while… Thanks guys.

EDIT: Forgot to say that the two non-working ones automatically instantiate 100 of blocks at the same position right away…

I think a bool might be what you want to use.

Like to say that if the locations are the same, then the bool is true, and then another if statement? Because that kind of seems like the same thing and that it wouldn’t help anything

I actually just figured out the problem. What was happening is that when I would instantiate that didn’t mean that the foreach would restart… It meant that it would go do it for as many times as it was run.

Thanks anyways!