If you've applied the script on 2 separate objects, then they are keeping track of 2 separate health amounts, and the sphere seems to be winning out in the text display. You want both scripts to reference the same health value. One way to do this is to create an Empty game object "Health" with this script "HealthScript" to hold that health value:
using System.Collections;
using UnityEngine;
public class HealthScript : MonoBehaviour {
public int currentHealth = 50;
}
And then adjust the pickup script:
using System.Collections; using UnityEngine; using UnityEngine.UI; public class PickupHealth : MonoBehaviour { public Text healthText; public int pickupAmount = 10; // Reference to HealthScript component in the Empty public HealthScript health; void Start() { healthText = GameObject.Find("Text").GetComponent(); // Assuming that the Empty is named "Health" health = GameObject.Find("Health").GetComponent(); } void Update() {
// Grab the health value from the HealthScript healthText.text = "Health: " + health.currentHealth; } void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Player") {
// Update the health value in the HealthScript health.currentHealth += pickupAmount; } } }