If you want to use more than one colour or if you change the colour of the “Package” the method Rick uses requires you have multiple variables for each package colour and then manually change each variable and object to reflect. This is code you can use to only change each “Package” colour and have it reflect without doing anything else.
bool hasPackage;
[SerializeField]float timeDelay = 1;
Color32 noPackageColour;
SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = GetComponent();
noPackageColour = spriteRenderer.color; // I use this as to get the colour of the Car directly
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == “Package” && !hasPackage)
{
Debug.Log(“Picked Up”);
hasPackage = true;
// The same method used to get the Car’s colour can be used to get the “Package” colour
// and then assign it directly to the car
spriteRenderer.color = collision.GetComponent().color;
Destroy(collision.gameObject, timeDelay);
}
else if (collision.tag == “Customer” && hasPackage)
{
Debug.Log(“Delivered”);
spriteRenderer.color = noPackageColour;
hasPackage = false;
}
}