Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trouble Building Chessboard

Discussion in 'Scripting' started by Dev_23, Sep 16, 2015.

  1. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    I have this code:
    Code (CSharp):
    1.     void BuildBoard() {
    2.  
    3.         for (int x = 0; x < 5; x++) {
    4.  
    5.             for (int y = 0; y < 5; y++) {
    6.  
    7.                 if (y % 2 == 0) {
    8.  
    9.                     Instantiate (BlackSquare, new Vector3( y + 2, x + 2, 0), Quaternion.identity);
    10.                 } else {
    11.  
    12.                     Instantiate (WhiteSquare, new Vector3( y + 2, x + 2, 0), Quaternion.identity);
    13.                 }
    14.             }
    15.         }
    16.     }
    17. }
    18.  
    I'm trying to instantiate squares that are 600 x 600 px, but I keep getting this:

    What's wrong with my code?

    Thanks in advanced
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's pretty much what you told it to do. For a chess board pattern try:

    Code (CSharp):
    1. void BuildBoard() {
    2.     for (int x = 0; x < 8; x++) {
    3.         for (int y = 0; y < 8; y++) {
    4.             if ((y + x) % 2 == 0) {
    5.                 Instantiate (BlackSquare, new Vector3( y + 2, x + 2, 0), Quaternion.identity);
    6.             } else {
    7.                 Instantiate (WhiteSquare, new Vector3( y + 2, x + 2, 0), Quaternion.identity);
    8.             }
    9.         }
    10.     }
    11. }
    For a more dry approach consider

    Code (CSharp):
    1. void BuildBoard() {
    2.     for (int x = 0; x < 8; x++) {
    3.         for (int y = 0; y < 8; y++) {
    4.             GameObject nextSquare;
    5.             if ((y + x) % 2 == 0) {
    6.                 nextSquare = blackSquare;
    7.             } else {
    8.                 nextSquare = whiteSquare;
    9.             }
    10.             Instantiate (nextSquare, new Vector3( y + 2, x + 2, 0), Quaternion.identity);
    11.         }
    12.     }
    13. }
     
    Dev_23 likes this.
  3. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18

    My new code is
    Code (CSharp):
    1. void BuildBoard() {
    2.  
    3.         for (int x = 0; x < 7; x++) {
    4.  
    5.             for (int y = 0; y < 7; y++) {
    6.  
    7.                 if (x % 2 == 0) {
    8.                     if (y % 2 == 0) {
    9.  
    10.                         Instantiate (BlackSquare, new Vector3( y * 2, x * 2, 0), Quaternion.identity);
    11.                     } else {
    12.  
    13.                         Instantiate (WhiteSquare, new Vector3( y * 2, x * 2, 0), Quaternion.identity);
    14.                     }
    15.                 } else {
    16.                     if (y % 2 == 0) {
    17.                      
    18.                         Instantiate (WhiteSquare, new Vector3( y * 2, x * 2, 0), Quaternion.identity);
    19.                     } else {
    20.                      
    21.                         Instantiate (BlackSquare, new Vector3( y * 2, x * 2, 0), Quaternion.identity);
    22.                     }
    23.                 }
    24.             }
    25.         }
    26.     }
    But yours is probably cleaner. My other problem is that the squares are overlapping, and I would like them not to.

     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Fixing the overlapping is relatively straight forward. Just multiply the x and y coordinates by the size of the game objects in your instantiate function.
     
    Dev_23 likes this.
  5. 10chaos1

    10chaos1

    Joined:
    Feb 13, 2014
    Posts:
    1
    wow i wish someone had linked me to this I had the same problem 3 months ago ,it was getting me depressed and this fixed it thank you every one that commented on this you are all awesome
     
    Last edited: Oct 27, 2015
    Kiwasi likes this.