Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by namelesskingXD · Jan 07 at 12:44 AM · 2d gamechess

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 :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirCrazyNugget · Jan 07 at 12:44 AM 0
Share

Do your clickable gameObjects have attached colliders?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image namelesskingXD · Jan 07 at 11:38 AM 0
Share

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?

avatar image henkehedstrom namelesskingXD · Jan 07 at 01:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

179 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges