So I am currently trying to code a chess game in unity 2d as a challenge. I am encountering an issue with the tilemaps where the two I have created do not line up.
My approach to creating the game is two have two tilemaps, one for the board generation of the squares and the other for the pieces which will be over the board. I have successfully done both of these however I can’t get the two tilemaps to be indexed the same. This will be how I interact with the pieces using the tile map indexes however the board has a different index to the pieces. This means that I have to translate it by a few squares in order for the click to be registered on the correct square or for the highlighting to be displayed correctly.
I am unsure of how to fix this as I have set all of the positions and tile anchors to 0 but they are still very off. Even when the camera is at 0,0 there is nothing on the display and the pieces are in the wrong place of the board?
As shown in the picture, the board is not positioned correctly and I’m not sure how to change it.
This is also what appears from the camera perspective.
This is not just an issue to do with this however, the simple fix would be to move it down 4 on the y axis but i still have issues with using the tile maps in code. For example if i was to click on the square 0,0 of the piece tilemap, when i go to colour the same square on the board tilemap it will be the wrong square.
I am very unsure of how to fix this so if there are any suggestions it would be great.
Thanks.
We know all these metrics work so you just have some offset in your scene or bug in your code. Here is how to track it down:
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Hi,
Thanks very much for your help. I’m still unsure of the error but I did end up using your approach of stripping it all out. I created a new project and decided to build it up from the beginning and have now found myself with no issues at all. Who knows what i must have done in order to incur the error!