- Home /
I need some Help creating a chess game please :)
Im new to unity and C# this is my first time using them, and i want to make chess game. So far i've managed to create the board, and instantiate my chess pieces. In my game scene i have to GameObjects Board and Pieces, Board has the createBoard() fucntion that creates the board, and it calls spawnPieces() function from Pieces. the problme im having right now is making click events to move those pieces. I've seen some tuto use OnMouseDwon method but its doesn't work for me
This is the Board script :
public GameObject[,] boardArray = new GameObject[8, 8];
void Start()
{
pieces = GameObject.FindGameObjectWithTag("PiecesScript").GetComponent<PiecesScript>();
createBoard();
pieces.spawnPieces(boardArray);
}
private void createBoard()
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if (x % 2 != 0 && y % 2 != 0 || x % 2 == 0 && y % 2 == 0)
{
boardArray[x, y] = Instantiate(whiteCell, new Vector3(x * 5.11f, y * 5.11f, 0f), Quaternion.identity);
}
else
{
boardArray[x, y] = Instantiate(blackCell, new Vector3(x * 5.11f, y * 5.11f, 0f), Quaternion.identity);
}
}
}
}
void OnMouseDown()
{
Debug.Log("clicked !");
Debug.Log(gameObject);
}
and this is my Pieces script :
public void spawnPieces(GameObject[,] board)
{
//spawning Pawns :
for(int i = 0; i < 8; i++)
{
board[i, 1] = Instantiate(whitePawn, new Vector3(i * 5.11f, 1 * 5.11f, -1), Quaternion.identity);
board[i, 6] = Instantiate(whitePawn, new Vector3(i * 5.11f, 6 * 5.11f, -1), Quaternion.identity);
}
//spawning Rooks :
board[0, 0] = Instantiate(whiteRook, new Vector3(0 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[7, 0] = Instantiate(whiteRook, new Vector3(7 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[0, 7] = Instantiate(blackRook, new Vector3(0 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
board[7, 7] = Instantiate(blackRook, new Vector3(7 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
//spawning Knights :
board[1, 0] = Instantiate(whiteKnight, new Vector3(1 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[6, 0] = Instantiate(whiteKnight, new Vector3(6 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[1, 7] = Instantiate(blackKnight, new Vector3(1 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
board[6, 7] = Instantiate(blackKnight, new Vector3(6 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
//spawning Bishops :
board[2, 0] = Instantiate(whiteBishop, new Vector3(2 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[5, 0] = Instantiate(whiteBishop, new Vector3(5 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[2, 7] = Instantiate(blackBishop, new Vector3(2 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
board[5, 7] = Instantiate(blackBishop, new Vector3(5 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
//spawning Queens :
board[3, 0] = Instantiate(whiteQueen, new Vector3(3 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[3, 7] = Instantiate(blackQueen, new Vector3(3 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
//spawning kings :
board[4, 0] = Instantiate(whiteKing, new Vector3(4 * 5.11f, 0 * 5.11f, -1), Quaternion.identity);
board[4, 7] = Instantiate(blackKing, new Vector3(4 * 5.11f, 7 * 5.11f, -1), Quaternion.identity);
}
Thank you so much in advince :)
Answer by henkehedstrom · Jan 07 at 10:35 AM
You currently have the OnMouseDown function on the board script which means that it will trigger if you click on the board object in the world. The reason that this doesn't work currently might be because you don't have a collider on the object with the board script.
I think you should create a script that every piece share(If you don't already have one).Then add the OnMouseDown function in that script. Then the function would trigger if you press any of the pieces.(Remember that you have to have a collider!)
Another idea would be to make all the cells clickable instead. And if you click a cell you check if that cell is currently holding a piece or not.
Thank u so much for ur respond.
What u said is true i have the OnMouseDown on my board GameObject, idk why i expected it to work on my prefabs x)
What do you think would be the best way to do this, is it to make a script and use AddComponent to pass it to all my prefabs ? or create a script with properties for every prefabs?
Here's a video that I think could help: https://www.youtube.com/watch?v=U4ogK0MIzqk Been a while since I watched it but I think it could give you some tips and such in creating a chess game in unity.
Your answer
Follow this Question
Related Questions
I need help with unity/webgl Canvases 0 Answers
How do I create 2D swirling wind patterns? (Use fluid dynamics?) 0 Answers
How to how to make a different background depending on the user inputed condition? 0 Answers
How can I create force on one end of an object using mouse dragg whose other end is fixed 1 Answer
How to fix graphic bugs LoadScene? 0 Answers