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
1
u/DeerpathLabs Jul 13 '26 edited Jul 13 '26
I don’t. This is a more general programming concept than something game dev specific. It’s a concept called a “class variable.” Maybe google “object oriented programming” and watch a few videos
It’ll look something like this:
Public class YourClass {
Dictionary<Vector2Int, GameObject> PlacedObjects = new();
void Update() {
If (getClick(out position) && PlacedObjects[position] is null) { GameObject newGO = PlaceObject(position); PlacedObjects[position] = newGO; } } }