this is probably really simple but i can’t seem to figure it out. i have a script for lives which adds a life after a delay of two seconds. when the total of lives amount to 5, then the game is over. the script works fine, but i would like to represent the number of lives as hearts on the screen during game runtime, instead of showing a number on the screen. so if the game-lives = 4 i would like four hearts to appear in a line… i’m not quite sure how to get this implemented. can someone please guide me to the process of doing this? this is a 2d game.
@Dblfstr - Same thing can be done using a world Quad and Material.SetTextureScale(). I was looking to give him a solution that only had a single draw call.
I have seen you answer many questions here. So your solution is probably more optimized than mine. That being said, what do YOU think of my solution above? Is that a practical way (sorry for asking questions in the comments) to achieve this?
void OnGUI(){
Rect r = new Rect(0,0, screen.width, screen.height); //Adjust the rectangle position and size for your own needs
GUILayout.BeginArea(r);
GUILayout.BeginHorizontal();
for(int i = 0; i < lives; i++)
GUILayout.Label(heartTexture); //assign your heart image to this texture
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
This is a good method as well. I just avoid OnGUI like the plague.
public SpriteRenderer heartHolder;
/*Drag your heart sprites here in the Inspector
You will need a sprite for 1 heart, two hearts, three hearts, etc.
Place them in order from one heart - highest number of hearts. */
public Sprite hearts[];
void start(){
//Grab the SpriteRenderer of this component
heartHolder = this.getComponent<SpriteRenderer>; //Something like that
}
void Update(){
/*Maybe make a bool here to
check if the lives have changed */
switch(gameLives){
case 1: {
heartHolder.sprite = hearts[0];
break;
}
case 2:{
heartHolder.sprite = hearts[1];
break;
}
case 3:{
heartHolder.sprite = hearts[2];
break;
}
case 4:{
heartHolder.sprite = hearts[3];
break;
}
case 5:{
heartHolder.sprite = hearts[4];
break;
}
default: {
heartHolder.sprite = null;
break;
}
}
}
I do the same thing for keeping score (took forever to figure out!) Number of stars you get for completing a level (1 -3), lives, etc. Once I figured it out, I use this method all over the place. The best part, I can keep all sprites on a sprite sheet so my Draw Calls for all GUI stuff is only 1.
I see how this works, and it is pure genius. Only show a portion of the texture depending on the number of lives. OnGUI though.
– Dblfstr@Dblfstr - Same thing can be done using a world Quad and Material.SetTextureScale(). I was looking to give him a solution that only had a single draw call.
– robertbuI have seen you answer many questions here. So your solution is probably more optimized than mine. That being said, what do YOU think of my solution above? Is that a practical way (sorry for asking questions in the comments) to achieve this?
– Dblfstrthanks guys! everything actually worked! like a charm!
– mealone@robertbu Thanks. That does make sense. Much better than the switch. I might have to change some of my own code now :/ lol
– Dblfstr