public void TakeDamage(float damage) { hpcurrent = Mathf.Clamp(hpcurrent - damage, 0.0f, hitpoint); if (hpcurrent == 0) { // remove its collider so the enemy won't got hit again triggerCollider.enabled = false; // tell the animator controller this unit is death animationController.Death(); // Add different collider rigidbody.isKinematic = true; // Death specific function if (func != null) { func(); } // if the enemy is already death, there is no point going further. this function end here. return; } if (hpbarHandle == null) { //instiate prefab hpbarHandle = Instantiate(hpbar); hpbarHandle.transform.SetParent(cam.transform.GetChild(0), false); hpbarHandle.SetActive(true); //set position Vector3 barPos = transform.position; barPos.y += hpBarOffset; hpbarHandle.transform.position = cam.WorldToScreenPoint(barPos); hpbarHandle.GetComponent <CanvasProperties>().SetTextMesh(0, enemyName); hpbarHandle.GetComponent <CanvasProperties>().SetAlpha(0.85f, hpBarDisplaySpeed, hpBarDisplayTime); hpbarHandle.GetComponent <CanvasProperties>().SetProgressorColor(1, hpBarFollowColor); hpbarHandle.GetComponent <CanvasProperties>().SetScaleX(triggerCollider.bounds.size.x * hpBarLengthFormula); } else { hpbarHandle.GetComponent <CanvasProperties>().SetAlpha(0.85f, hpBarDisplaySpeed, hpBarDisplayTime); } }