All the enemies in my game have health and on collision with the player, they subtract their current level of health from the players.
The problem is that if the player reduces one of their health to zero, all of them disappear. At the same time, if I set their health to private variable, the health won't sutbract from the enemies health on collision.
public var Health: int = 100;
function OnCollisionEnter(other: Collision){
if(other.gameObject.CompareTag("Player")){
other.gameObject.Health -= Health;
Destroy(gameObject);
rigidbody.velocity = -rigidbody.velocity;
}
try using a public variable, we had similar problems and ours is now a public variable, however I did do a few other things which I can't recall (chopping and changing solutions got all mixed up) so it may or may not work. :(
This question is really confusing, almost every use if 'their' or 'health' is ambiguous. You're reducing player health by enemy health each time an enemy collides with a player? The code looks straightforward. I don't understand your problem.
I'm not sure if this is exactly what you're looking for, but it should at least give you an idea as to how to go about modifying variables in other scripts without using static vars. If this script is attached to an enemy object any time it comes into contact with the player it will subtract it's current health from the player then self destruct.
var enemyHealth : int;
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.CompareTag("Player"))
{
var tempScript = col.gameObject.GetComponent("name of script that handles health");
tempScript.health -= enemyHealth;
Destroy(gameObject);
}
}
Small error: var tempScript = col.GetComponent("name of script that handles health"); Is suppose to be: var tempScript = col.gameObject.GetComponent("name of script that handles health");
Same meaning. Static means one copy for the entire class, which means what you said -- if you zero out "your" health, you are really zeroing the single health we all share. Quoting your answer, above "MAKE SURE IT'S NOT STATIC." As long as it is public or has a public get, there won't be any "can't interact" issues.
Even if you say that, it's still not working. It doesn't alter the health based on the remainder of the other game objects health on collision if the health is a public variable.
try using a public variable, we had similar problems and ours is now a public variable, however I did do a few other things which I can't recall (chopping and changing solutions got all mixed up) so it may or may not work. :(
– anon61858128That didn't work. It could call the Health from the other scripts.
– PersonaThis question is really confusing, almost every use if 'their' or 'health' is ambiguous. You're reducing player health by enemy health each time an enemy collides with a player? The code looks straightforward. I don't understand your problem.
– flaviusxvii