Changing old GUI health code to new Unity UI

Iv been using a GUI script for health for awhile now but I want to convert to the new Unity UI and I can’t figure out how. I keep getting errors when I switch things to have the new UI labels like “Text” etc. Can anyone help me out? I want to be able to use the new Unity UI so I can have my GUI be in world space canvases. Here is my code its also C#

using UnityEngine;
using System.Collections;
 
//ATTACH TO MAIN CAMERA, shows your health and coins
public class GUIManager : MonoBehaviour
{      
        public GUISkin guiSkin;                                 //assign the skin for GUI display
        [HideInInspector]
        public int coinsCollected;
 
        private int coinsInLevel;
        private Health health;
       
        //setup, get how many coins are in this level
        void Start()
        {
                coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;               
                health = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
        }
       
        //show current health and how many coins you've collected
        void OnGUI()
        {
                GUI.skin = guiSkin;
                GUILayout.Space(5f);
               
                if(health)
                        GUILayout.Label ("Health: " + health.currentHealth);
                if(coinsInLevel > 0)
                        GUILayout.Label ("Cubes: " + coinsCollected + " / " + coinsInLevel);
        }
}

Unity UI doesn’t exactly work like that, that’s a whole different story, you’re no longer tied to OnGUI, you’re now tied to objects, because new UI’s are components.

You can clone behaviour of your “style”, but I would strongly recommend to not do it that way, let me try to explain why, here are the bare bones of how your clone would ~look like:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

//ATTACH TO MAIN CAMERA, shows your health and coins
public class GUIManager : MonoBehaviour
{
    [HideInInspector]
    public int coinsCollected;

    private int coinsInLevel;
    private Health health;
    private Text yourHealthText;
    private Text yourCubesText;
    //setup, get how many coins are in this level
    void Start()
    {
        coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;
        health = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
        yourHealthText = GameObject.Find("yourHealthTextObject").GetComponent<Text>();//or public/[SerializeField] and assign it through inspector instead or GameObject.FindObjectsOfType<Text> and loop through them to find or...
        yourCubesText = GameObject.Find("yourCubesTextObject").GetComponent<Text>();//or public/[SerializeField] and assign it through inspector instead or GameObject.FindObjectsOfType<Text> and loop through them to find or...
    }

    //show current health and how many coins you've collected
    void Update()
    {
        if (health) yourHealthText.text = "Health: " + health.currentHealth;
        if (coinsInLevel > 0) yourCubesText.text = "Cubes: " + coinsCollected + " / " + coinsInLevel;
    }
}

First of all - performance, unless your health and coins incr/decr faster than frames come out then you’ll have performance loss. As instead you could create some function that will repeat itself in X time and update text or create 2 separate functions for incr/decr health and coins that will set respective values + update the text, and here we come to a second point - why would you get objects, incr/decr their values and display text not in the respective object in the first place, or in the function where you incr/decr respective values? I guess that’s enough of explanations and you already see where I’m going.

So what should you do? If you don’t mind all of the above^ then go for that example that I’ve shown and correct it for your taste (obviously referencing to your actual texts, etc.), but if you want to better understand and get in touch with new UI, I’d suggest you to check out Unity’s UI tutorials and Unity’s UI examples. Also there are plenty of 3rd party UI health bar examples and tutorials with different concepts on the web, you can use your favourite search engine to find them out.