Stops destroying packages

When I add “&& !hasPackage” to the first IF statement it stops destroying the packages when running over. Everything works fine if I remove that part. I have even tried “hasPackage = false”. Just tried with just “hasPackage”. It destroyed the package.

public class Delivery : MonoBehaviour
{
    [SerializeField] bool hasPackage;
    private void Start() 
    {
        Debug.Log(hasPackage);
    }
    [SerializeField] float destroyDelay = 0.5f;

    void OnCollisionEnter2D(Collision2D other)
    {
        Debug.Log("Ouch!");
    }

    void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Package" && !hasPackage)
        {
            Debug.Log("Package picked up.");
            hasPackage = true;
            Debug.Log(hasPackage);
            Destroy(other.gameObject, destroyDelay);
        }   

        if (other.tag == "Customer" && hasPackage)
        {
            Debug.Log("Packaged delivered.");
            hasPackage = false;
        }
    }
}

Ok, so after more testing it seems like the first package in front of my car won’t be destroyed until I grab another one first.

I figured it out. Somehow, I must have hit ctr-d and added a second package on the same spot. So, it was picking up the first one.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms