// Every frame: private void Update() { barrier = GameObject.FindGameObjectWithTag("barrier").GetComponent <barrierHealth>(); health = barrier.health; healthPercentage = int.Parse((Mathf.Round(maximumHealth * (health / 100f))).ToString()); // If the player's health is below the minimum health, then set it to the minimum health: if (health < minimumHealth) { health = minimumHealth; } // If the player's health is above the maximum health, then set it to the maximum health: if (health > maximumHealth) { health = maximumHealth; } // If the character's health is not full and the health regeneration button is ticked, regenerate health/sec at the rate of 'healthPerSecond': if (health < maximumHealth && regenerateHealth) { health += healthPerSecond * Time.deltaTime; // Each time the health is changed, update it visibly: UpdateHealth(); } }
private void Update() { if (!found) { barrierHealth = GameObject.FindGameObjectWithTag("barrier").GetComponent <barrierHealth>(); if (barrierHealth != null) { found = true; } } if (found) { Sethealth(barrierHealth.health); } }