- Home /
How to find the layer of child ?
I'm trying to use layers to check if my character is on the ground or not. I tried this :
void OnCollisionEnter2D (Collision2D other)
{
if (other.gameObject.layer == 8) {
onGround = true;
}
}
but this doesn't work on chlidren. And my colliders are in children. I tried other things like use Physics2D.OverlapCircle instead of other.gameObject.layer, but it didn't worked. (may be I used it in a wrong way)
There is other.gameObject.transform.FindChild ("GROUND").tag which is working with children, unfortunately this use .tag and didn't find a way to use it with .layer
If anyone understood my problem and can solve it, could you please explain me how ?
Answer by steakpinball · Mar 26, 2015 at 02:31 PM
other.transform.Find("GROUND").gameObject.layer
seems to be what you want.
He'd be so happy to see this haha. $$anonymous$$oral : layer is public variable of gameObject and not the transform. Anyway, braintumer can you answer my question ? I added pics :3
Answer by Owen-Reynolds · Mar 26, 2015 at 03:34 PM
I'm not sure about Collision2D, but in regular 3D collision, other.gameObject
and other.transform
are different. other.gameObject
is the top parent of whatever you hit, and other.transform
is the actual object.
So, it might be other.transform.gameObject.layer
The idea is, you might have a compound collider that "counts" as just one object. You only ever care about the main object. So collision purposely looks up all the way to the top for gameObject. Just to make things easier for most people. Then it also tells you the real thing, in transform, just in case.
Your answer
