Public int not changing from inside a collision method.

I’ve been trying to find out why the “current_Treasure” isn’t changing when I try to from the collision method.
This is the entire script. Nothing else changes anything here.

public int max_Treasure;
    public int current_Treasure;

    void Start()
    {
        max_Treasure = GameObject.FindGameObjectsWithTag("Treasure").Length;
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
       
        if (collision.gameObject.CompareTag("Player"))
        {
            current_Treasure += 1;
            Destroy(gameObject);
        }
    }

You’re destroying this object after the collision. It doesn’t matter if you changed the variable because it’s going to cease to exist as soon as the object is destroyed.

2 Likes

Thanks, turns out it was that. I’ve put the variable into a different script and called it. Now it works.