So I have some code (lets say codeA) referencing a static variable (var) in a separate code (codeB). CodeB is applied to multiple instances. CodeA changes var and then a function in codeB is started. My problem is that I want that function to happen only in whatever instance is being affected currently, but instead it starts the function on all the instances the code is applied to. CodeA:
var attack: int = 20;
var enemy: GameObject;
var enemyHealth: EnemyHealth;
function Awake ()
{
enemyHealth = GetComponent(EnemyHealth);
}
function OnTriggerEnter(obj: Collider)
{
if(obj.gameObject.GetComponent(EnemyHealth))
{
enemyHealth.currentHealth = enemyHealth.currentHealth - attack;
}
}
CodeB:
var startingHealth: int = 100;
static var currentHealth: int;
var enemy: GameObject;
private var onDeath: PlayerMovement;
private var anim: Animator;
function Awake()
{
onDeath = GetComponent(PlayerMovement);
currentHealth = startingHealth;
anim = GetComponentInParent(Animator);
}
function Update()
{
if(currentHealth <= 0)
{
anim.SetBool("EnemyDeath", true);
Destroy(enemy, 2);
onDeath.enemyDie = true;
}
}
↧