what is the simplest way to get player selection system like checkers in unity 3d c#

what is the simplest way to get player selection system like checkers in unity 3d c#

like lets say the board is a top down grid like if your were in front of an actual checkers board i want to figure out how i would be able to select/deselect a piece then select/deselect a space on the board so that the players piece will move to said place and if another piece is occupying that place already then the said player cant move there
also im new to c# and progrming in general...i have been only doing it for about five months and have been looking at tutorials and all that ... but if some one could paste an example code if possible something simple so that i can dissect it, learn from it, and change it as I see fit. 
and if you can explain how the code works that would be really appreciated.
things i need:
some kind of grid/checker board code 
player selection/deselection 
i would prefer it in c# and the code applicable strictly to unity3ds mono develop although I intend to learn visual studio in the future at the moment i just want something for unity.
lastly please assume that i may be stupid when explaining/writing your example code/ or answer.


Answer 1:

To begin with, in order to access and update your board with ease, you can make use of the Dictionary<Key, Value> class together with the Vector2 structure defined by Unity.

Dictionary<Vector2, Tile> tiles = new Dictionary<Vector2, Tile>();

Tile is a class defined by you. It should contain information such as its coordinates on the board, and whether it is occupied by any entity etc. You can define a simple Tile class as follows.

using UnityEngine;
using System.Collections;

public class Tile : MonoBehaviour {
    public Vector2 coordinates;
    public ChessPiece chessPiece;

    // add other fields as you want to

Now, let’s wrap things up by a Board class.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Board : MonoBehaviour {
    public Dictionary<Vector2, Tile> tiles = new Dictionary<Vector2, Tile>();

    // add other functions later

Of course, you need chess pieces to play with.

using UnityEngine;
using System.Collections;

public class ChessPiece : MonoBehaviour {
    // add other functions later

The management of the board should be centralized with the presence of a “Manager”. This “Manager” is in charge of handling the transition of states of the board. Again, this “Manager” should be implemented by yourself.

using UnityEngine;
using System.Collections;

public class BoardManager : MonoBehaviour {
    public Board board;

    void Awake() {
        GameObject boardObject = new GameObject("board");
        boardObject.transform.parent = transform;
        board = boardObject.AddComponent<Board>();

    // add other functions later

Colliders are often used by different physics-related functionalities. For instance, in a game of chess, each chess piece should have a collider attached to it. The collider, in this case, is mainly used for receiving user input, such as tapping and dragging. There are many excellent plugins available in the Unity Asset Store for handling different kinds of user input/gestures. By using these plugins, you are being freed from handling those tedious input-related logic. For example, there is certain plugin which allows to write functions like this.

// a function for handling 'tapping' on a chess piece
void OnTap(Gesture gesture) {
    if (gesture.pickObject == gameObject) {
        // process 'tapping' on this chess piece...

Of course, the code snippet above covers only the basic usage of these plugins. You should refer to the official manuals provided by the authors of those plugins for more detailed explanations.

To summarize, you should at least have the following stuff prepared:

  • Manager, responsible for managing the board
  • Dictionary, storing the layout of the board
  • Chess Piece, represents each individual chess piece on the board
  • Tile, represents each individual tile on the board


Here is a screenshot which shows a hierarchy of GameObject in a simple chess game.

sample hierarchy of objects in game

Of course, you don’t need to follow it strictly. The structure can be very flexible. It is really important to experiment with different structures, so as to find a way to handle gaming logic which you feel comfortable while giving good performance.