private void Update() { timeSinceAttack += Time.deltaTime; if (targets.Count <= 0) { return; } bool canAttack = (timeSinceAttack > _attackSpeed); if (canAttack) { List <GameObject> deadTargets = new List <GameObject>(); foreach (GameObject target in targets) { if (!target) { deadTargets.Add(target); } else { UnitHealth unitHealth = target.GetComponent <UnitHealth>(); unitHealth.TakeDamage(_damage); } } if (deadTargets.Count > 0) { foreach (GameObject deadTarget in deadTargets) { targets.Remove(deadTarget); } } timeSinceAttack = 0; } }
void Awake() { unitHealth = GetComponentInParent <UnitHealth>(); barMask = GetComponentInChildren <RawImage>(); healthBar = GetComponentInChildren <Image>(); uiCanvas = GetComponent <Canvas>(); if (unitHealth && healthBar) { unitHealth.onHealthChanged += OnHealthChanged; } uiCanvas.worldCamera = Camera.main; }
private void CreateNewUnitIcons(List <GameObject> selectedUnits) { DestroyOldIcons(); foreach (GameObject o in selectedUnits) { if (!o) { return; } GameObject newIcon = Instantiate(unitPanelPrefab, transform); UnitHealth health = o.GetComponent <UnitHealth>(); UnitPanelHealthBar uHealth = newIcon.GetComponent <UnitPanelHealthBar>(); if (!health || !uHealth) { return; } uHealth.Initialize(health); } }
public void Initialize(UnitHealth health) { _health = health; health.onHealthChanged += UpdateUiHealth; }