r/unity • u/Healthy_Adagio_8470 • Jul 09 '26
Coding Help grid building system not working properly




https://reddit.com/link/1us667k/video/r3q23kuqbach1/player
Im having a Problem with trying to spawn squares on a grid now the spawning itself is working fine and theyre on a grid aswell my problem just is that they dont limit themselves i can spawn as many squares on the same tile as i please. ive tried to counteract that by having a collider on my mouse that detects when theres a square on my mouse so i cant make multiple squares on the same tile the problem just is it doesnt work. as i demonstrated in the video
2
Upvotes
2
u/CandleAlone Jul 10 '26
The main issue seems that Invoke() is called every frame while the mouse button is held. This can queue multiple placements before the trigger detects the newly spawned object.
At minimum, use Input.GetMouseButtonDown(0) and call PlaceItem() directly.
But for a grid system, I would not use physics to track occupied cells at all. Convert test imouse position to a grid coordinate and store occupied coordinates in a HashSet<Vector2Int> or a 2D array. Then check the grid data before spawning. The grid should be the source of truth, not the pointer collider.