Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

When I make my board bigger than 3x3 I can't find the cells when I click on them.

Discussion in '2D' started by pepijn0328, Jul 27, 2021.

  1. pepijn0328

    pepijn0328

    Joined:
    Oct 15, 2018
    Posts:
    17
    I am making a sort of tic-tac-toe/noughts and crosses game. I have made a function that can find a cell if it is within a distance of 0.4999 (all the cells are a distance of 1 apart from each other).
    When I have a board of 3x3 cells this works fine. But when I make my board bigger it kind of stops working. When I do a 4x4 board I can only find the two lower rows and the first cell of the third row (first picture).
    When I do a 5x5 board I can only find the lower row and the first four cells of the second row (second picture)
    When I do a 6x6 board I can only find the lower row and the first three cells of the second row (third picture)

    So it looks like when I make the board bigger I can find two cells less each time? But at 3x3 it works fine so I have no idea what causes it.

    Here is my Board code:

    Code (CSharp):
    1. public class Board : MonoBehaviour
    2. {
    3.     public GameObject cCellPrefab;
    4.     public static Dictionary<int, Cell> cellDictionary = new Dictionary<int, Cell>();
    5.  
    6.     public void Create()
    7.     {
    8.         int cellCounter = 0;
    9.         for(int y = 0; y < 6; y++)
    10.         {
    11.             for(int x = 0; x < 6; x++)
    12.             {
    13.                 GameObject newCell = Instantiate(cCellPrefab);
    14.  
    15.                 cellDictionary.Add(cellCounter, newCell.GetComponent<Cell>());
    16.                 cellDictionary[cellCounter].Setup(new Vector2(x, y), this, false, cellCounter);
    17.                 cellCounter++;
    18.             }
    19.         }
    20.     }
    21. }
    And here is my cell search code:

    Code (CSharp):
    1.     public static Cell FindMouseCell(Vector2 position)
    2.     {
    3.         Cell cell;
    4.         for (int i = 0; i < 9; i++)
    5.         {
    6.             cell = Board.cellDictionary[i];
    7.             if (Vector2.Distance(cell.pos, position) < 0.4999f)
    8.             {
    9.                 Debug.Log("Close cell = " + cell.cellInt);
    10.                 return cell;
    11.             }
    12.         }
    13.         Debug.Log("ERROR No close cell has been found error!");
    14.         return null;
    15.     }
    And here is my find mouse position code:

    Code (CSharp):
    1.     private Vector2 FindMouse(Camera cam)
    2.     {
    3.         Vector3 mouseWorldPos = cam.ScreenToWorldPoint(Input.mousePosition);
    4.         mouseWorldPos.z = 0f;
    5.         Debug.Log("mousePos = " + mouseWorldPos);
    6.         return mouseWorldPos;
    7.     }
    And here I have pictures of how the board looks with all the cells I can find clicked on. Every cell that has been found has a black or grey thing on it, this is just for indication which cells I can and can't find.
    picture1.png picture2.png picture3.png
     
    Last edited: Jul 27, 2021
  2. pepijn0328

    pepijn0328

    Joined:
    Oct 15, 2018
    Posts:
    17
    nevermind I figured it out, I am stupid and I have no idea why I saw it sooner. I only looped over a maximum of 9 cells because at the moment of writing the looping code I had only 9 cells and wasn't planning on expanding.


    btw, if anyone sees this, how do I add the 'solved' tag thing in front of my title and when it isn't solved the 'help needed' tag thing in front of my title. Thanks in advance!