need help with my health script
var health = 300;
function Update ()
{
}
function OnCollisionEnter (Hit : Collision)
{
if (Hit.transform.gameObject.tag == "Zombies")
health -= 20;
if(health == 0)
{
Application.LoadLevel("Game Over Screen");
}
}
function OnGUI()
{
GUI.Box(Rect(20,20,health,20), "Health");
}
its not giving me any compiler errors its just i have my zombies tagged exactly as Zombies just my character doesnt like getting killed or something it was working just fine earlier idk what happened i didnt even touch the script? or you could just help me make a new one or somethin idk
if you didn't touch the script the problem must be somewhere else. have you changed collider types, our the way all your entities move?make sure the script really works by putting a
Debug.Log(Hit.collider.tag);
to it
i put the debug in and indeed it is not working? and nothing is changed for colliders
I added another capsule collider to my character and it started working? idek what was going on
It's possible that either your Zombie or Player's collider has been set as a trigger. This would make it not call the OnCollisionEnter() function. Check to make sure that the "Is Trigger" parameter in the inspector is unchecked on both your Zombie and Player.
Answer by Tony_T · Sep 14, 2015 at 09:23 AM
You need to provide more information in order for us to help you. What do you want to achieve with this script? Now, there are a few wrong things with your script. First of all you are checking if the health is 0 in the collision enter function which is wrong. You need to check that in the update function, so it checks the health every frame. Also you never give the minimum limit for the health, so it can go below 0 (-1, -2, etc.). I did some changes to your script, try it and let me know (I'm not home to test it myself).
var health = 300;
function Update ()
{
//Checking if health is below zero.
if (health < 0)
{
//If it is, make it zero.
health = 0;
}
if (health == 0)
{
Application.LoadLevel("Game Over Screen");
}
}
function OnCollisionEnter (Hit : Collision)
{
if (Hit.transform.gameObject.tag == "Zombies")
{
health -= 20;
}
}
function OnGUI()
{
GUI.Box(Rect(20,20,health,20), "Health");
}