If statement with two colliders

I’m very new, but how do I solve this problem of making two game objects trigger the ability to instantiate another once placed in a trigger. Ex: cheese + pizzaw/sauce = pizza. When I try to use &&, the if statement only needs the first object.

void OnTriggerStay(Collider other)
{
if (other.gameObject == cheese && pizzaSauce)
{
Debug.Log(“Lit”);
Instantiate(pizza);
pizza.transform.position = new Vector3(6.6f, 1.2f, 2.6f);
Destroy(cheese);
Destroy(pizzaSauce);
counter.SetActive(false);

}
}

You need to have a list of objects that are within the trigger area. To do this, create an empty list and add objects to it when they “enter” the trigger (OnTriggerEnter), and remove objects from the list when they “exit” the trigger (OnTriggerExit). Therefore, after the next OnTriggerEnter, go through the list of objects and if it contains the necessary set (pizza recipe), create a pizza and clear the list of used objects.

2 Likes

Let’s break this statement down. The part before the “&&” is checking if other.gameObject is equal to cheese but the part after the “&&” is checking if pizzaSauce exists not if other.gameObject equals pizzaSauce. The correct way to write that line would have been:

if (other.gameObject == cheese && other.gameObject == pizzaSauce)

Of course it’s irrelevant in this case as other.gameObject can only be a single object not multiple objects so even with the correctly written line it still wouldn’t work for the reason mentioned in the previous post.

1 Like

Oh alright, that’s great to know. Thanks for the solution!

Yeah I see now, thanks for clarifying!