kodeco.com Forums

How to Make a Chess Game with Unity

Not every successful game involves shooting aliens or saving the world. Board games, and chess, in particular, have a history that spans thousands of years. In this tutorial, you’ll build a 3D chess game in Unity.

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5441-how-to-make-a-chess-game-with-unity

Hi Brian Broom, Nice tutorial in unity…can you tell me How to make rummy like a game in Unity…

I don’t think we’ve done any card games (traditional or otherwise) - I can add it to the list to consider.

Thanks for your consideration…

Hi Brian,
Thanks for this awesome tutorial. Just what I was looking for!
Question(s) on meshes.
How are the pieces created?
I see the prefabs (How are those created?)
and I see the meshes (also, how are those created?)

How was the board created to be 8x8?

I didn’t see how these were done programatically.

I have a new bend on this timeless classic and I am in need of expanding the board, not only by tile count but also in function. The board has the ability to change during game play.
Can you point me to the tutorials that might answer what I want to do?

Again, love the tutorial. Very well made!



The meshes were created by Eric Van de Kerckhove with (I think) Blender. 3d modeling is not my strength, so I can’t really answer questions about that process. The meshes were exported from Blender (fbx, I think) and dragged into Unity.

The prefabs are created by an empty node, with the mesh as a child. I learned this trick somewhere. Its pretty common for 3d software to have different coordinate systems than Unity, so the imported meshes may be (eg) sideways or otherwise rotated from how you want them. Having the mesh as a child of a blank node lets you fine tune that rotation, scale, or position and have the mesh centered nicely on that node.

In this case, the board was a single block mesh, with a texture for the black and white squares. I have it defined as 8 x 8 in GameManager. You could build up the board as tile prefabs, and the size would be more explicit, thats just the assets I was working with.

One other set of tutorials I like is Unity 3d: Making a Board Game! - Episode 1 - YouTube - there are a couple of board game playlists there. If there are specific styles or mechanics you would like to see here, I’m a big fan of board game style games - let me know and we’ll see what we can do.

Hope that helps

Hi Brian,
Thank you for this awesome chess tutorial. I’ve managed to get it running and even made a few modifications. I’m struggling mightily, however, with implementing an indication that a King is in check. The goal is to simply render a material on the King in check when he’s in check. I’ve managed to create a script that will generate a List of opposing pieces and their next move coordinates, compare those to the King’s coordinates - if they match - King is in check. Actually implementing that into the game is proving to be challenging.
Can you tell us what approach you used (or would use) to implement something similar. I’m thinking to get my newScript to fire on the TileSelector change in state, but no joy.
Thanks again for this excellent tutorial.

Hi Brian,
When I added the tile selector script, it keeps saying this:

NullReferenceException: Object reference not set to an instance of an object
Board.DeselectPiece (UnityEngine.GameObject piece) (at Assets/Scripts/Board.cs:64)
GameManager.DeselectPiece (UnityEngine.GameObject piece) (at Assets/Scripts/GameManager.cs:133)
MoveSelector.ExitState () (at Assets/MoveSelector.cs:62)
MoveSelector.Update () (at Assets/MoveSelector.cs:43)

Please help me solve this, because I have no idea what is going on
Zachary (AKA Zac)

Edit: I fixed it!

@bcbroom Can you please help with this when you get a chance? Thank you - much appreciated! :]

There are many ways to set this up, but the way I approached it was to have the “states” be waiting for different user inputs (waiting to pick a piece, waiting to pick a target square). Everything else was something that happened when a new state started or ended.

I think the easiest thing would be to put it in the EnterState method of TileSelector. Call your method to see if the current player King is in check, and display something in the UI.

There are several other places you could put it - I think of mechanics like this in terms of - what state transition makes sense for this action. Even though there isn’t much real difference between the exit of state 1 and the enter of state 2, I find that usually one seems to make more sense in my mental model, and I go with that.

Hope that helps

Glad its working - my guess for NullRef issues is usually that I forgot to add a reference to something in the inspector window, which happens all the time.

Less often, I’ve called a method that I’m expecting to return something, and nothing matched.

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!