- Home /
When player touches this collider then health is subtracted
So i have a "health" script and I'm not sure if it's made in a weird way or not, but that isn't the question.
I want to make it so if the player comes into contact with another object it loses a certain amount of health (kind of like a Super Mario turtle in a way) my "health" script looks like this.
var player : GameObject;
var health = 1000;
function Start()
{
}
function Update()
{
if(health <= 0)
{
Debug.log("you are dead");
Destroy(player);
}
if(health >= 1000)
{
health = 1000;
}
}
How would you recommend creating the other script on another object that can find the health script on the player and subtract health?
thank you :)
Answer by Graham-Dunnett · Feb 07, 2015 at 06:18 PM
Thanks that seems like it will work, but I've run into a problem while trying to subtract the value of 100 from my health value which is set to 1000. When i use
function OnCollisionStay2D(coll: Collision2D)
{
if(coll.gameObject.tag == "Enemy")
{
health - 100;
}
}
i get an error that states "Expressions in statements must only be executed for their side-effects." I have the health set as a value of 1000. I don't know what this means and how I should go about solving this. Any recommendations?