Hi, I have a script that handles my character’s health, and now Im trying to get a second script to access this value and display it on the script, here’s what I’ve got (and isnt working):

public class PlayerHealth : MonoBehaviour
{
    public float health = 100f;                        
    public float resetAfterDeathTime = 5f;             
    public AudioClip deathClip;                         
 ...   

And here’s the GUI script:

using UnityEngine;
using System.Collections;

public class GUIscript : MonoBehaviour 
{
	private PlayerHealth playerHealth; //Reference to the playerHealth component.
	
	void OnGUI () {
		GUI.Box (new Rect (15,15,150,20), "Health: " + playerHealth.health); 
	}
}

This looks like it should work, although I believe you may have forgotten to initialize playerHealth.

If you make this member variable public you can drag and drop the PlayerHealth component onto the GUIscript component to initialize it. Alternatively you could initialize it through code in the Start, OnEnable or Awake function (or somewhere else ;)).

If you did not forget this, please share what error is getting thrown at you.