so I have a system that add matches when I pick them and removes them when you light a candle, and when you run out you can no longer light candles. I have a UI script that supposed to display a updated number based on match count.
every part works except the unity display
I keep getting this error :
NullReferenceException: Object reference not set to an instance of an object
FirstPerson.AddMatches (System.Single matches) (at Assets/MyScript/FirstPerson.cs:64)
PickupMatch.OnTriggerStay (UnityEngine.Collider target) (at Assets/MyScript/PickupMatch.cs:1
pickupmatch Script:
void OnTriggerStay(Collider target)
{
if (target.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
txt.text = " ";
match.SetActive(false);
FirstPerson.AddMatches(5);
}
}
}
FirstPerson Script
[Header("Matches Parameters")]
[SerializeField] private static float currentmatches;
private static UIScript uiscript;
void Start()
{
UIScript uiscript = GetComponent<UIScript>();
}
public static void AddMatches(float matches)
{
currentmatches = currentmatches + matches;
uiscript.UpdateMatches(currentmatches);
}
public static void MinusMatches(float matches)
{
currentmatches = currentmatches - matches;
}
public static float GetMatches()
{
return (currentmatches);
}
lightacandle
void OnTriggerStay(Collider target)
{
matches = FirstPerson.GetMatches();
if (target.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E) && matches > 0)
{
txt.text = " ";
fire1.SetActive(true);
FirstPerson.MinusMatches(1);
}
}
}
UI script
[SerializeField] private TextMeshProUGUI healthText = default;
[SerializeField] private TextMeshProUGUI staminaText = default;
[SerializeField] private TextMeshProUGUI matchText = default;
private float matches;
private void OnEnable()
{
FirstPerson.OnDamage += UpdateHealth;
FirstPerson.OnHeal += UpdateHealth;
FirstPerson.OnStaminaChange += UpdateStamina;
UpdateMatches(matches);
}
private void OnDisable()
{
FirstPerson.OnDamage -= UpdateHealth;
FirstPerson.OnHeal -= UpdateHealth;
FirstPerson.OnStaminaChange -= UpdateStamina;
}
private void Start()
{
UpdateHealth(100);
UpdateStamina(100);
matches = 0;
}
private void UpdateHealth(float currentHealth)
{
healthText.text = currentHealth.ToString("00");
}
private void UpdateStamina(float currentStamina)
{
staminaText.text = currentStamina.ToString("00");
}
public void UpdateMatches(float currentmatch)
{
matchText.text = currentmatch.ToString("00");
}
}