- Home /
2 Colliders on 1 gameobject
Hello,
I want to be able to have 2 colliders on a gameobject but both being affected by 2 different actions.
Meaning:
Imagine a circle in the air(with 2 circle colliders on). The first collider will be 5x bigger than the circle, the second collider being 2x bigger than the circle. Now if the player collides with the first circle, it will spawn a 'missile' that will fire at the circle and if that 'missile' collides with the second collider, it will destroy the circle and itself. How can i give a title to the two different colliders for the ability i am trying to do to function properly. Like giving that collider a tag or something.
Answer by GesterX · May 16, 2011 at 12:40 PM
I am 99% sure you could do this:
- Give your gameObject two empty child GameObjects
- Attach a different collider to each child object
- Write two scripts with your OnColliderEnter functions for each collider
- Attach each script to each child object
So in the end you would have:
-Parent GameObject with:
-Child A with Collider A (large collider) and Script A (missile spawning) attached
-Child B with Collider B (small collider) and Script B (destroy on collision) attached
thank you, this was easier than the way i was planning on doing it. Only thing is i am working with a few prefabs, would this make a difference to anything?
No worries glad to help. Depending on how you're using the prefabs you may need multiple ones.
Answer by DeltaTwoZero · Feb 26, 2019 at 01:37 AM
What i would also consider is to create 1 script with 2 references (for each collider). Afterwards, you could use:
GameObject ChildGameObject1 = ParentGameObject.transform.GetChild (0).gameObject;
GameObject ChildGameObject2 = ParentGameObject.transform.GetChild (1).gameObject;