Friday, June 17, 2022
HomeGame Developmentc# - Unity reference randomly turns into null

c# – Unity reference randomly turns into null


This difficulty has stumped be for two days and I nonetheless cannot determine it out.
I’ve a leveling script that references and picture and subscribes to a world occasion.

public class LevelSystem : MonoBehaviour
{
    [SerializeField] Picture _levelProgressBar;

    void Begin()
    {
        GameEvents.OnPickUpExp += AddExp;
    }

    public void AddExp(int quantity)
    {
        // Right here _levelProgressBar turns into null on random playthroughs
        _levelProgressBar.fillAmount = (float)_experience / _experienceToNextLevel;
    }
}

and an merchandise pickup script that triggers the occasions earlier than destroying itself

public class ItemPickup : MonoBehaviour
{
    [SerializeField] int quantity;

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Participant"))
        {
            GameEvents.TriggerPickUpExp(quantity);
            Destroy(gameObject);
        }
    }
}

Fairly easy stuff. However I am unable to for the lifetime of me work out why the picture reference turns into null randomly.
What I’ve tried

  • Double examine that I did not have multiple occasion of the LevelSystem part
  • Checking that my division by no means leads to a division by zero error
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments