I just noticed an issue with my grids and I can't figure out where to check

So, it seems that for whatever reason in my grid, it’s trying to put two of the units on the tiles at the same time except for the initial unit at 0,0. Meaning that when I move the 2nd unit off of 2,2 it will still be listed as occupied and can’t be selected to return to that spot.

Where do I need to go to fix t his issue, as it’s been a while since i last looked through the grid script.

Maybe you’re manually calling Unit.Start(); which runs it twice and adds the Unit twice?
Look at all the places where you’re adding the Unit to the LevelGrid, it should only be called once, after that it should be only the movement code that modifies the LevelGrid.

When you move a Unit, does it move both references? Or one stays “behind”?

I’ll take a look.

And it’s moving one of the unit references, for both the enemy and the other player. one of them stays behind making it so that when they move the spot is unable to be used again, but only on their initial positions.

I looked through my code and i’m not calling start twice anywhere and i’m not really seeing anywhere i’d be calling the addunitatgridposition function anywhere else. It’s only listed in start, and is called again in the unitmovedgridposition function.

One thing i did notice is that if i comment out the initial call in start, it does remove one of the two times it’s added to the other units, but removes the only reference for the first unit.

Did you manually look or did you use Visual Studio to help you find the references? Right click on top of AddUnitAtGridPosition and “Find References”
Add a Debug.Log(); inside AddUnitAtGridPosition, with the Start enabled it should only fire 3 times, not 5.
The only two possible explanations is you’re calling that function somewhere where you shouldn’t, or you have the same code running (adding Unit to the grid) but not through that function.

I manually checked to see where else it would be getting called and when using the Find References tool, I was only finding it twice. Once in the start in the Unit.cs. And the other time being in UnitMovedGridPosition in the Levelgrid.cs.

And from the screenshot you can see I did add the debug.log, it it is indeed getting called 5 times.

I did just notice something.

I decided to add another debug in the unit.cs update in the if (newGridPosition != gridPosition) to see how often it was getting called. And when running the game initially I’m seeing my initial “has been called” message triggering 3 times. Then the new debug i placed in update, then the addunit debug, then another update debug, and finally the 5th addunit debug.

I figured it out.

This is the issue.

It looks like because i was creating a new ‘gridPosition’ here after it was already created up above, doing it once again in Start() was causing it to run the AddUnitAtGridPosition multiple times for some of the units.

Removing that bit of code gave the expected results.

Oh yup that would be it, by making that at local variable means the class variable gridPosition stays on 0,0 but the Start still adds the units to the correct grid position.
Then when the Update runs it will think those units moved from 0,0 to where they currently are so it removes them from 0,0 and adds them a second time to the current grid position.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms