You could make the card a gui.button, or put a transparent button over it at least, and decrease the health var when it gets clicked.
Then iterate the amount of hearts to display one next to the other each time.
here is what the code could look like:
public class YourClass : MonoBehaviour
{
public int _life = 6;
public Texture2D _cardImage;
public Texture2D _hearthImage;
void OnGUI()
{
Rect groupRect;
Rect heartRect;
Rect cardRect;
groupRect= new Rect(0, 0, 180, 300);
heartRect = new Rect(0, 0, 30, 30);
cardRect= new Rect(0, 30, 180, 270);
GUI.BeginGroup(groupRect);
//Draw the card
if (GUI.Button(cardRect, this._cardImage))
{
--this._life;
}
//Then draw the hearts
for (int i = 0; i < this._life; ++i)
{
GUI.DrawTexture(heartRect, this._hearthImage);
heartRect.x += heartRect.width;
}
GUI.EndGroup();
}
}
By the way, how could i add to this code a condition for the 2nd row of hearts.
I mean that, if there will be more than 6 hearts, they will be displayed on the 2nd row like on the image below:
The math is simple, you divide the life variable by the number of hearts in a row and add 1 to have the number of rows. Then take the number of rows into account for the button position then check for row switching in the current heart loop.
The code would be:
public class YourClass : MonoBehaviour
{
public int _life = 6;
public Texture2D _cardImage;
public Texture2D _hearthImage;
public int _heartWidth = 30; //This is a bonus to make things more dynamic ;)
public int _heartHeight = 30; //This is a bonus to make things more dynamic ;)
public int _heartsPerRow = 6;
public int _cardHeight = 270;
void OnGUI()
{
int rowCount;
Rect groupRect;
Rect heartRect;
Rect cardRect;
//Calculate the row count and the rects
rowCount = Mathf.CeilToInt((float)this._life / this._heartsPerRow);
//Make sure the group is big enough, btw you can change the X and Y of this variable to place the card anywhere you need
groupRect= new Rect(0, 0, this._heartsPerRow * this._heartWidth, this._cardHeight + this._heartsPerRow * this._heartHeight);
heartRect = new Rect(0, 0, this._heartWidth , this._heartHeight);
cardRect= new Rect(0, this._heartHeight * rowCount, groupRect.width, this._cardHeight); // Place the card below the rows
GUI.BeginGroup(groupRect);
//Then draw the hearts
for (int heart = 0; heart < this._life; ++heart)
{
//Check if we reached a new row
if (heart > 0 heart % this._heartPerRow == 0)
{
//Place the heart rect at the beginning of the row
heartRect.x = 0;
heartRect.y += this._heartHeight;
}
GUI.DrawTexture(heartRect, this._hearthImage);
heartRect.x += heartRect.width;
}
//And finally draw the card
if (GUI.Button(cardRect, this._cardImage))
{
--this._life;
}
GUI.EndGroup();
}
}