I am making chess in unity2D and am trying to set the pieces as children of the squares as shown in the function below, however it is not creating new game objects with no errors being output.
private void DrawPiece(int piece, Transform parentSquare)
{
if (!pieceSprites.ContainsKey(piece))
{
Debug.LogError($"Piece sprite not found for piece code: {piece}");
return;
}
string pieceBinary = Convert.ToString(piece, 2).PadLeft(5, '0');
GameObject pieceObj = new GameObject($"Piece_{pieceBinary}@{parentSquare.position.x * 8 + parentSquare.position.y}");
pieceObj.transform.position = parentSquare.position;
pieceObj.transform.parent = parentSquare;
SpriteRenderer sr = pieceObj.AddComponent<SpriteRenderer>();
sr.sortingOrder = 1;
sr.sprite = pieceSprites[piece];
pieceObj.transform.localScale = new Vector3(pieceSize, pieceSize, 1);
}
I tried setting the pieceObj parent to transform and it generates the piece fine.
Output with pieceObj.transform.parent = parentSquare;
Output with pieceObj.transform.parent = transform;
Any help would be much appreciated 🙂