I have spent days trying to find a way to access the square my chess pieces are on for a game I am making in Unity. What I want to do is click on a chess piece and have its square returned to me. I am currently trying to do this by getting the coordinates of the square that I click on and checking the value in a dictionary. Every time, however, it returns null. My classes are as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Game : MonoBehaviour
{
public GameObject white_king, white_queen, white_rook, white_bishop, white_knight, white_pawn;
public GameObject black_king, black_queen, black_rook, black_bishop, black_knight, black_pawn;
public static bool whiteMove;
public static bool blackMove;
public string[] squareNames = {"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8",
"b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8",
"c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8",
"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8",
"e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8",
"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8",
"g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8",
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
public static Dictionary<string, Coordinates> squares = new Dictionary<string, Coordinates>();
public static Dictionary<Coordinates, string> square_references = new Dictionary<Coordinates, string>();
private double x = -3.94;
private double y = -3.92;
public void Start()
{
whiteMove = true;
blackMove = false;
for (int i = 0; i < 64; i++)
{
if (i % 8 == 0 && i > 0)
{
y = -3.94;
x += 1.1;
}
squares.Add(squareNames[i], new Coordinates(x, y));
square_references.Add(new Coordinates(x, y), squareNames[i]);
y += 1.1;
}
Instantiate(white_queen, new Vector3((float) squares["d1"].getX(), (float) squares["d1"].getY(), -2), Quaternion.identity);
Instantiate(white_king, new Vector3((float) squares["e1"].getX(), (float) squares["e1"].getY(), -2), Quaternion.identity);
Instantiate(white_rook, new Vector3((float) squares["a1"].getX(), (float) squares["a1"].getY(), -2), Quaternion.identity);
Instantiate(white_rook, new Vector3((float) squares["h1"].getX(), (float) squares["h1"].getY(), -2), Quaternion.identity);
Instantiate(white_bishop, new Vector3((float) squares["c1"].getX(), (float) squares["c1"].getY(), -2), Quaternion.identity);
Instantiate(white_bishop, new Vector3((float) squares["f1"].getX(), (float) squares["f1"].getY(), -2), Quaternion.identity);
Instantiate(white_knight, new Vector3((float) squares["b1"].getX(), (float) squares["b1"].getY(), -2), Quaternion.identity);
Instantiate(white_knight, new Vector3((float) squares["g1"].getX(), (float) squares["g1"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["a2"].getX(), (float) squares["a2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["b2"].getX(), (float) squares["b2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["c2"].getX(), (float) squares["c2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["d2"].getX(), (float) squares["d2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["e2"].getX(), (float) squares["e2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["f2"].getX(), (float) squares["f2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["g2"].getX(), (float) squares["g2"].getY(), -2), Quaternion.identity);
Instantiate(white_pawn, new Vector3((float) squares["h2"].getX(), (float) squares["h2"].getY(), -2), Quaternion.identity);
Instantiate(black_king, new Vector3((float) squares["d8"].getX(), (float) squares["d8"].getY(), -2), Quaternion.identity);
Instantiate(black_queen, new Vector3((float) squares["e8"].getX(), (float) squares["e8"].getY(), -2), Quaternion.identity);
Instantiate(black_rook, new Vector3((float) squares["a8"].getX(), (float) squares["a8"].getY(), -2), Quaternion.identity);
Instantiate(black_rook, new Vector3((float) squares["h8"].getX(), (float) squares["h8"].getY(), -2), Quaternion.identity);
Instantiate(black_bishop, new Vector3((float) squares["c8"].getX(), (float) squares["c8"].getY(), -2), Quaternion.identity);
Instantiate(black_bishop, new Vector3((float) squares["f8"].getX(), (float) squares["f8"].getY(), -2), Quaternion.identity);
Instantiate(black_knight, new Vector3((float) squares["b8"].getX(), (float) squares["b8"].getY(), -2), Quaternion.identity);
Instantiate(black_knight, new Vector3((float) squares["g8"].getX(), (float) squares["g8"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["a7"].getX(), (float) squares["a7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["b7"].getX(), (float) squares["b7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["c7"].getX(), (float) squares["c7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["d7"].getX(), (float) squares["d7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["e7"].getX(), (float) squares["e7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["f7"].getX(), (float) squares["f7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["g7"].getX(), (float) squares["g7"].getY(), -2), Quaternion.identity);
Instantiate(black_pawn, new Vector3((float) squares["h7"].getX(), (float) squares["h7"].getY(), -2), Quaternion.identity);
}
}
public class Coordinates
{
public double x;
public double y;
public double getX()
{
return this.x;
}
public double getY()
{
return this.y;
}
public Coordinates(double x, double y)
{
this.x = x;
this.y = y;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Linq;
public class Piece : MonoBehaviour
{
public GameObject moveSquare;
private bool isDragging;
Vector2 startPosition;
string square;
void Start()
{
startPosition = transform.position;
}
private void OnMouseDown()
{
if((getColour().Equals("white") && Game.whiteMove) || ((getColour().Equals("black") && !Game.whiteMove)))
{
isDragging = true;
CreateMoveSquare("f5");
Debug.Log(getSquare());
}
}
private void OnMouseUp()
{
if((getColour().Equals("white") && Game.whiteMove) || ((getColour().Equals("black") && Game.blackMove)))
{
isDragging = false;
DestroyMoveSquares();
if(getColour().Equals("white"))
{
Game.whiteMove = false;
Game.blackMove = true;
}
else
{
Game.whiteMove = true;
Game.blackMove = false;
}
}
}
void Update()
{
if(isDragging)
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(mousePosition);
}
}
private string getColour()
{
switch (this.name)
{
case "black_queen 1(Clone)": return "black";
case "black_knight(Clone)": return "black";
case "black_bishop(Clone)": return "black";
case "black_king(Clone)": return "black";
case "black_rook(Clone)": return "black";
case "black_pawn(Clone)": return "black";
}
return "white";
}
private string getSquare()
{
string myKey = Game.squares.FirstOrDefault(x => x.Value.x == (double) transform.position.x && x.Value.y == (double) transform.position.y).Key;
return myKey;
}
public void DestroyMoveSquares()
{
GameObject[] movePlates = GameObject.FindGameObjectsWithTag("MoveSquare");
for (int i = 0; i < movePlates.Length; i++)
{
Destroy(movePlates[i]);
}
}
public void CreateMoveSquare(string square)
{
GameObject ms = Instantiate(moveSquare, new Vector3((float) Game.squares[square].getX(), (float) Game.squares[square].getY(), -3), Quaternion.identity);
}
}
In this instance Debug.Log(getSquare()) gives me null no matter which pieces I click on. I have tried using square_references to get the the key through mykey = Game.square_refenreces[new Coordinates[transform.position.x, transform.position.y] but it also returns null. I have tried many other things but nothing seems to work. Does anyone know why or what I can do to make this work?