I'm trying to pick up a magazine and then have it disappear then display a certain amount of number on the screen.
So when I try to pick up the magazine it doesn't work and there's a certain problem with the Object reference not set to the object
Here's the code for the value to be added and shown on screen:
public class GlobalAmmo : MonoBehaviour
{
public static int CurrentAmmo;
public GameObject AmmoDisplay;
void Update()
{
AmmoDisplay.GetComponent<Text>().text = "" + CurrentAmmo;
}
}
Here's the error that's been showing:
NullReferenceException: Object reference not set to an instance of an object GlobalAmmo.Update () (at Assets/Scripts/wepons/GlobalAmmo.cs:11)
I think there's a problem with me using the TextMeshPro UI Component instead of using the regular text component but as the newer version of unity doesn't have that feature so can someone point me out what's the code for textmeshpro as i tried but there's not an object like that
Try to get TextMeshProUGUI
component:
AmmoDisplay.GetComponent<TextMeshProUGUI>().text = "" + CurrentAmmo;