Package Picked Again and again

Hi, when we delivered the package the console does not print again a package delivered message due to the bool false as we use in the code but when we picked the package once and we haven’t delivered yet when i again goes form the package then the console again prints the msg that you picked the package. I need to change that when we picked the package once until when we deliver that package new msg not be come on when we move form that package because we already picked the package once.

Simplest solution (in line with the lecture you’re on) is to just check if you already have the package when you pick it up. The code is the same as the customer except that you’d check if hasPackage is false (not true)

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

    // .. customer 'if' here

We add the check for ‘not hasPackage’ (!hasPackage) to pick it up. You could also do it more explicitly

if (other.tag == "Package" && hasPackage == false)
{
    // ...
}

Now you’ll only print the message if you drive over the package and you don’t already have it

1 Like

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

Privacy & Terms