C++ How to make a UI Text display the same number as a Variable

Hello! So I have a gun script where I want it to display the amount of ammo left but all of the things I have found online are all in Javascript. The one I found in C++ was this:

public Text example;
private int currentAmmo;

void Update() {

example = currentAmmo;

}

But when I tried this I got this error:

Cannot implicitly convert type int' to UnityEngine.UI.Text’

Someone please help! Thanks!

To modify the display of the UI Text you need to modify the text property.

Since example.text expects a string you need to convert the int to a string - currentAmmo.ToString();

So to update the text display every frame you could do:

void Update() {
    example.text = currentAmmo.ToString();
}