I’m making a chess game and I want to set the icon automatically based on what PieceType I have.
Here’s what I’ve been trying to do in the Piece-class:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Piece
{
//Basics
public Texture2D icon;
public enum PieceType
{
King, Queen, Bishop, Knight, Rook, Pawn
}
public enum PieceSide
{
White, Black
}
//Position
public string StrPos;
public int NumPos;
public Piece myPiece = new Piece();
public void UpdatePiecePosition(string stringPart, int NumberPart)
{
}
public void SetPieceIcon()
{
if (myPiece.PieceType == PieceType.King)
{
}
}
}
Hi, @TheRichardGamer
Add to the piece class:
public PieceType type;
pubic PieceSide side;
//Later on in program
if ( (INSERT PIECE).type == Piece.PieceType.King) {
(...)
}
I see you have defined your enumerations but haven’t used them. This was a problem for me when I first started using them. So your overall script will be:
[System.Serializable]
public class Piece
{
//Basics
public Texture2D icon;
public enum PieceType {King, Queen, Bishop, Knight, Rook, Pawn}
public enum PieceSide {White, Black}
public PieceType type;
public PieceSide side;
//Position
public string StrPos;
public int NumPos;
//What is this doing here?
public Piece myPiece = new Piece();
public void UpdatePiecePosition(string stringPart, int NumberPart)
{
}
public void SetPieceIcon() {
if (this.type == PieceType.King)
{
//THIS IS HOW TO CHECK
}
}
}
And for your number position, I recommend you use the data type Vector2, because it will allow you to do things in the future of a chess game which you may find very useful.
I commented a section of the code: //What is this doing here?
I done so because you are creating an infinite loop of declarations of the object “Piece”, which will crash unity. You also don’t have a constructor for “Piece”, so add something like this to your program:
public Piece (PieceType _type) {
this.type = _type;
}