0

So I've got a collider that's checking for the tag of the game object that enters it. If both a green and ref tag enter it I want to call a separate function.

I have it working for individual game objects but I'm having problems with multiple. This is all happening inside a OnTriggerEnter2D.

I'm not getting any errors with it but the debug.long never prints so the collider isn't seeing the gameobjects.

Thanks in advance! :)

        else if (trigCol.name == "Yellow Trigger")
        {
            if ((collision.CompareTag("greenBlock")) && (collision.CompareTag("redBlock")))    //  If the tag of the colliding object is greenBlock
            {
                Debug.Log("Inside yellow trigger. This is the " + collision.tag + " tag");
                YellowBlock();
            }
            else if (collision.tag == "blueBlock")
            {
                Debug.Log("Incorrect block!");
                wrongSound.Play();
                currentScene = SceneManager.GetActiveScene();
                SceneManager.LoadScene(currentScene.name);
            }
        }
1
  • 2
    Trigger callbacks happen one trigger at a time, so you need to track the redblocks/greenblocks in a variable in your monobehavior. You can either save the blocks in Lists, or increment/decrement a counter for each color when they leave or enter the trigger. Nov 30, 2019 at 15:15

1 Answer 1

1

collisionis only one value, the OnCollisionEnterfunction gets called each time there is a collision.

Try using variables that are set in OnCollisionEnter and OnCollisionExit to keep track of when two objects are both colliding with the referenced object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.