In my game I have a group of objects with the same tag and a trigger collider. When the player collides with any of these objects, a parameter within the player is changed and that works fine. (This is done in a script attached to the player).
Now in a script attached to each object, I want to have it that if the player collides with that object, a parameter of that single object is changed.
The problem is when I use OnTriggerEnter, I cannot specify whether the collision is between the player and the specific object. I cannot check if the colliding gameobject has the object’s tag cuz all the objects in this group have the same tag. And when I check if the colliding object has the ‘player’ tag, the function is called when the player collides with any object, not just the specific object the script is attached to.
I have searched so long for an answer, so I hope someone here can help me.
Here’s some of the code simplified.
// Function in Player Script
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Object")
{
object = true;
}
}
// Function in Object Script
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
player = true;
}
}