- Home /
Load level when hp = 0
Hello. I have a simple combat game with 2 oponents but when the life bar is = to "0" nathing happens. I Want that when life bar of "bad" character = to 0 load a new level.
HP SCRIPT:
using System.Collections; /// /// HP & MP Manager /// public class HpManager : MonoBehaviour { // HP bar, MP bar public UISlider hpBar, mpBar; // Max State public int hpMax = 100; public int mpMax = 100; // Current State int hp = 100; int mp = 100;using UnityEngine;
// Init HP State public void InitHp() { SetHp(hpMax); } // Init MP State public void InitMp() { SetHp(mpMax); } // Set Damage on HP State public void DoDamageHp(int point) { SetHp(hp - point); } // Set Recover on HP State public void DoSaveHp(int point) { SetHp(hp + point); } // Set Recover on MP State public void DoSaveMp(int point) { SetMp(mp + point); } // Set HP State public void SetHp(int point) { hp = Mathf.Clamp(point, 0, hpMax); if (hpBar) hpBar.value = (float)hp / (float)hpMax; } // Set MP State public void SetMp(int point) { mp = Mathf.Clamp(point, 0, mpMax); if (mpBar) mpBar.value = (float)mp / (float)mpMax; } }
You might want to try something like:
public void SetHp(int point)
{
hp = $$anonymous$$athf.Clamp(point, 0, hp$$anonymous$$ax);
if (hpBar)
hpBar.value = (float)hp / (float)hp$$anonymous$$ax;
if(transform.tag == "BadGuy" && hp <= 0)
{
// The following will reload the current level!
// $$anonymous$$ake sure you add it as a build scene.
Application.LoadLevel(Application.loadedLevel);
}
}
Just make sure your opponent has the "BadGuy" tag!
P.S. There are better ways to do this, but this is probably the fastest way
ALL THE SCRIPTS ARE WOR$$anonymous$$ING, WHEN THE BAD AND GOODGUY LOSE LIF$$anonymous$$
THAN$$anonymous$$S A LOT!
THAN$$anonymous$$ YOU :))))))
O$$anonymous$$G! Finnallly!!
Thank you so much for your help!
It works!
Thanks a lot!
You should accept the correct answer so this question turns a delightful green ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
how to link objects in the inspector on load level 1 Answer
connect to hosted level 0 Answers
Loadlevel can't see each other. 2 Answers