How do I check if the current PieceType is a King?
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)
{
}
}
}
Answer by Fydar · Mar 03, 2016 at 08:42 PM
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
}
}
}
Also, please look into Properties for the string position, so you only have to store a number position, here is a link: https://msdn.microsoft.com/en-gb/library/x9fsa0sw.aspx
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;
}
Ok, I'm done with the first part. However, about setting the icon... when I try to set the icon as a sprite and apply it to a sprite-renderer, nothing happens. Why? The original textures are certainly PNG but it says that they are Sprites in the inspector.
Here's my code: using UnityEngine; using System.Collections;
public class $$anonymous$$ing : $$anonymous$$onoBehaviour {
private Piece.PieceSide mySide;
public Piece king;
public Sprite myIcon;
void Start ()
{
if (this.mySide == Piece.PieceSide.White)
{
king = new Piece (Piece.PieceType.$$anonymous$$ing, mySide, "Icons/white_king.png");
myIcon = (Sprite)Resources.Load ("Icons/white_king.png");
this.gameObject.GetComponent<SpriteRenderer> ().sprite = (Sprite)myIcon;
}
if (this.mySide == Piece.PieceSide.Black)
{
king = new Piece (Piece.PieceType.$$anonymous$$ing, mySide, "Icons/black_king.png");
myIcon = (Sprite)Resources.Load ("Icons/black_king.png");
this.gameObject.GetComponent<SpriteRenderer> ().sprite = (Sprite)myIcon;
}
}
}
Hey, @TheRichardGamer. I simplified the $$anonymous$$ing script, it is more clear and efficient.
public class $$anonymous$$ing : $$anonymous$$onoBehaviour {
private Piece.PieceSide mySide;
public Piece king;
public Sprite myIcon
void Start() {
string imagePathName = "Icons/" + ( ( mySide == Piece.PieceSide.White )
? "white_king.png" : "black_king.png" );
king = new Piece( Piece.PieceType.$$anonymous$$ing, mySide, imagePathName );
myIcon = ( Sprite )Resources.Load( imagePathName );
this.gameObject.GetComponent< SpriteRenderer >().sprite = myIcon;
}
}
@Fydar 's list of possibilities are right. I can't spot anything that may cause the problem you are having.
I think there are 2 possibilities:
1: The if statements aren't working, have you set the value of "mySide"? It is private so I doubt you have set it in the editor.
2: The Resources.Load isn't setup correctly.
Apart from that, I don't see how it isn't working. Sorry.
Your answer
![](https://koobas.hobune.stream/wayback/20220612070452im_/https://answers.unity.com/themes/thub/images/avi.jpg)