- Home /
I created a system that tracks number of matches have
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");
}
}
Answer by lvskiprof · May 29 at 02:32 AM
A few things I can't verify here...
What is "default" that you are setting healthText, staminaText, and matchText to?
These should not be set in the script, but in the Unity Editor. Have you done that?
In the UIScript, lightacandle, and pickupmatch scripts, where is FirstPerson being set so you can use it (assuming it is supposed to be a reference to the class, as you seem to be using the wrong case for variable and class names here)? Also, where are OnDamage, OnHeal, and OnStaminaChange declared in the FirstPerson script? They appear to be public variable the way you are using there here, but normally they should be changed using a method to set them. For that matter, what are the UpdateDamage, UpdateHealth, and UpdateStamina values you are adding or subtracting on OnEnable() and OnDisable()?
By having the methods and the uiscript variable declared as 'static', every instance of the FirstPerson class shares that variable and those methods, but if there is more than one instance of the UIScript class you will run into trouble. Whatever the last FirstPerson class instance is that runs is what the single uiscript variable will be set to.
Your answer
Follow this Question
Related Questions
Please Help figure out whats wrong with Ammo Counter. 2 Answers
Can't drag text object into inspector? 0 Answers
counting how many times a specific animation plays 3 Answers
Collision counter 2 Answers
Why Is TextMesh Off by One ??? 0 Answers