- Home /
How to make a trigger only affect the collided if its parents name is x
Hello!
I am trying to make a laser that, when it collides with an object that has a parent names Asteroids, print hi. The following code doesn't work however, does anyone know how to fix this?
Thanks!
private void OnCollisionEnter2D(Collision2D collision) { if (collider.transform.parent.tag = "Asteroids") { print("hi"); } }
Use collision in place of collider variable and use == for comparing the values: private void OnCollisionEnter2D ( Collision2D collision ) { if (collision.transform.parent.tag == "Asteroids") { print("hi"); } } I think it should solve your problem
Answer by Larry-Dietz · Dec 26, 2017 at 04:54 AM
`try changing it to this...
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.transform.parent.tag == "Asteroids")
print("hi");
}
-Larry
Your answer
Follow this Question
Related Questions
How to make an object move with a script which isn't attached to it? 2 Answers
Make a player only move in one direction 1 Answer
Moving a non-player object in a random direction which bounces off other objects. 2 Answers
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
Physics.CheckSphere always returns true when it shouldn't 1 Answer