Does anyone know how to fix my issue? I have text that will not update with my ammo. It gives me this error in the console: “NullReferenceExeption: Object reference not set to an instance of an object” (I took out the important parts in the script down below.)
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
public class GunScript : MonoBehaviour
{
public int maxAmmo = 100;
private int currentAmmo;
Text ammoTxt;
void Awake()
{
ammoTxt = GetComponent<Text>();
}
void Start()
{
maxAmmo = 100;
currentAmmo = maxAmmo;
}
void Update()
{
ammoTxt.text = gameObject.GetComponent<GunScript>().currentAmmo.ToString();
}
public void Shoot()
{
currentAmmo--;
}
}