- Home /
How to use OnTriggerEnter on different gameobject to detect collision?
I have an empty GameObject called collider with collider2D with trigger on I have an Sprite called Ball with rigidbody and Collider2d aswell I also have empty gameobject called ColliderScript to my collider script. My question is how to use tags to detect the collision between collider and Ball and print out something if my script being attach to collider script. I tried tags and nothing works, nothing printed out.
void OnTriggerEnter2D(Collider2D col)
{
player2Collider = GameObject.FindGameObjectWithTag("Player2Collider");
Debug.Log(" Worked");
}
Answer by PizzaPie · Mar 25, 2017 at 01:55 PM
void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.tag == "Ball")
{
//add the code you want to execute on collision
//to access the Ball gameObject use : col.gameObject
}
}
attach this scipt to the collider GameObject and add the tag "Ball" to the Ball GameObject
Answer by ThePhantomGoat · Jul 14, 2018 at 07:08 PM
im pretty sure in 3D its the same, but without the "2D" after some words.
I tried
void OnTriggerEnter(Collider col) { if(col.gameObject.tag == "CheckPoint") { Debug.Log("CheckpointHitted"); } }
but it doesnt register the hit. do you know any other method? or where did this method go wrong?