This issue has stumped be for 2 days and I still can't figure it out. I have a leveling script that references and image and subscribes to a global event.
public class LevelSystem : MonoBehaviour
{
[SerializeField] Image _levelProgressBar;
void Start()
{
GameEvents.OnPickUpExp += AddExp;
}
public void AddExp(int amount)
{
// Here _levelProgressBar becomes null on random playthroughs
_levelProgressBar.fillAmount = (float)_experience / _experienceToNextLevel;
}
}
and an item pickup script that triggers the events before destroying itself
public class ItemPickup : MonoBehaviour
{
[SerializeField] int amount;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
GameEvents.TriggerPickUpExp(amount);
Destroy(gameObject);
}
}
}
Pretty simple stuff. But I can't for the life of me figure out why the image reference becomes null randomly. What I've tried
- Double check that I didn't have more than one instance of the LevelSystem component
- Checking that my division never results in a division by zero error