- Home /
Restart the game when Health variable reaches 0
This is the script I am using. I did not make this script! Basicly, I want the game to restart when the health reaches 0. I have very little scripting experience, but am happy to learn. Anyone know how I might be able to achieve this?
@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;
var health : int = 100;
function OnGUI () {
// At first we draw border/background texture (with current texture it isn't important)
GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
//Now on top we can put Health Bar texture
var adjust : int = health * 3; //adjusting texture size (width) / health(100)
GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}
Answer by Maulik2208 · Jan 03, 2013 at 05:57 AM
void Update()
{
if(health <= 0)
{
Application.LoadLevel(/*here write number of the level (or name of the level) which you want to load*/)
}
}
Enjoy.....Cheers......IF found useful then don't forget to mark the answer......
Thanks! worked exactly how I wanted it! Didn't know it was that simple. Again, Thanks.
Glad that your problem is Solved.....have a happy coding....Cheers
Answer by Piflik · Jan 02, 2013 at 07:56 PM
Do you want to restart the current level, or start over from the beginning?
Use Application.LoadLevel("Level"), where 'Level' is the name or index of the level you want to load (if you use the index, don't use quotation marks). If you want to restart the current level, you can write Application.LoadLevel(Application.loadedLevel).
What would I use to trigger this? I tested it out with a Input.Get$$anonymous$$eyDown.
function Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)) {
Application.LoadLevel (0);
}
}
But how do I make this run when the health of the player reaches 0?
Use whatever event you use to reduce the player's health. Just add an if statement to test, if the new health value is below 1 and if so, load the level.
Then you should take care of that first...you can't load a level when the player's health drops to 0, if the player's health is never reduced.
Answer by ytanay · Jan 02, 2013 at 09:02 PM
Why not simply add an Update() function that checks if the health is below 0 and runs Application.LoadLevel()
accordingly?
Here is the full code:
@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;
var health : int = 100;
function OnGUI () {
// At first we draw border/background texture (with current texture it isn't important)
GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
//Now on top we can put Health Bar texture
var adjust : int = health * 3; //adjusting texture size (width) / health(100)
GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}
function Update () {
if(health<0){
Debug.Log("Player is dead");
Application.LoadLevel("Your scene");
}
}
Hope this helps!
If you want to restart the current level using this method, you could do Application.LoadLevel(Application.loadedLevelName)
How would I do that? (I dont have much experience with scripting)
It's surprisingly simple. I updated my answer with the full code.
Answer by Jozxyqk · Jan 03, 2013 at 06:17 AM
Before loading the level it'd be nice to give the player some time to realize what just happened. For that you could create a simple timer:
float restartTimer;
bool gameOver = false; //changing this to a more general "game state" later on will give more control
void Update()
{
if (health < 0 && !gameOver)
{
//happens once, as state changes to game over
restartTimer = 4.0f;
gameOver = true;
}
if (gameOver)
{
gameTimer -= Time.deltaTime; //count down when in gameOver state
if (gameTimer < 0.0f) //add '&& Input.GetButtonDown("Fire1")' to allow user to choose when to restart, but stops them accidentally clicking and missing a game over message
{
Application.LoadLevel(Application.loadedLevel); //or whatever level you want
//update the state so LevelLoad doesn't keep being called. Probably not needed if this script gets destroyed anyway
gameOver = false;
health = 100;
}
}
}
... then later in your OnGUI
if (gameOver)
{
//draw game over message
}
... Don't forget to disable some player controls so you can't keep playing when in the gameOver state
There may be other timing methods (I'm not a fan of the yield method) but this should work.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Acces to a var from another Script. 2 Answers
insert semicolon 2 Answers
set another scripts variable 2 Answers
How much memory does a pointer use? 5 Answers