- Home /
Static, yet Private, Variables?
I have my script done so that it spawns enemies which the player can fight and they will attack him. The problem is that because the enemies use the same script there's no way to indicate if the player's health has gone down without the collision defining whether he's been hit or not is static. Don't get me? Here's what I mean:
If 2 enemies are attacking the Player, because the hitTime is static, only 1 will hit him.
How can I change it so that both enemies will hit the Player?
Variable
static var hitTimeWeak:boolean = false;
Enemy Script:
function Attack()
{
isAttacking = true;
if(isAttacking == true && beingHit == false)
{
yield WaitForSeconds(WeakAttackSpeed);
hitTimeWeak = true;
animation.CrossFade("attack1");
yield WaitForSeconds(.5);
hitTimeWeak = false;
yield WaitForSeconds(.8);
animation.Stop();
hitTimeWeak = false;
isAttacking = false;
}else{
isAttacking = false;
}
}
Player Getting Hit:
function OnTriggerEnter(hitWeak : Collider)
{
if(hitWeak.gameObject.tag == "StalfosWeak" && Enemy1.hitTimeWeak == true)
{
HealthBar.HEALTH -= 1;
}
}
Answer by Eric5h5 · Mar 02, 2011 at 03:38 AM
You can't use static variables like that; static means that only one instance can ever exist. Don't use static variables unless you mean for that to happen. So remove "static" and just use normal variables, in which case each instance of the script will have its own independent variables.
I understand, but then whats the alternative in order to make each enemy hit him?
@Jordan: You don't need an alternative, just remove "static". Unless you're talking about accessing other scripts, in which case http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
What I mean is, because all enemies share the script, they will all share static variables, so If one's attacking, they're all attacking, and if I take out the static part, then I can't access it from the character script to inform him that he's been hit and needs to take damage. Currently, if they hit at the same time, 1 will hit, but the other won't because the hitTimeWeak variable is in use.
@Jordan: As I said, you have to remove "static" from the variables and make them non-static. You can't use static variables if you need more than once instance. You can easily access non-static variables from other scripts, see the link I just posted.
So this will tell me if at anytime the hitTimeSmall variable in the Enemy1 script is true, the character will get hurt, if applied correctly?