Reputation: 313
I am having big troubles with one simple task. I want when I detect collision check if the tag of the parent game object is equal to my value. Because I want to add a special effect only when colliding with that obstacle.
This is my hierarchy:
A - > PARENT
-B -> PARENT CHILD
--C -> Collider Gameobject
So I want when my player collides with C object to check is the tag in A equal to my value but I don't know how to get the tag of the PARENT A gameobject.
Thank you for your time :)
Upvotes: 6
Views: 2894
Reputation: 2485
@Sean Carey's answer works perfectly when you only want to go up one level in the hierarchy, but will fail in your particular case, seeing as you appear to be looking to check the tag of the 'root' Transform
.
Luckily Unity has provided us with a property to reference the root from any given Transform
object.
Here's an example of how you might use it but consider changing it to suit your specific needs:
private void OnCollisionEnter(Collision collision)
{
if (collision.transform.root.CompareTag("EnterTagToCompareHere"))
{
// Tag on the root object matches
}
}
Upvotes: 6
Reputation: 807
private void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.parent.CompareTag("Enemy"))
{
//Do stuff
}
}
Upvotes: 3