- Home /
How to: Click top Object of 2 overlapping objects (2D)
I want an easy way to click the top gameObject of two overlapping gameObjects -2D only please- Example: Two clickable 2D objects overlap, gameObject A (top) and gameObject B (bottom). gameObject A has a higher z-axis and is visibly on top, but only gameObject B is clickable. Object A is higher but only B ic clickable. B was there first and is the Background of my scene.
I've seen a similar question here, http://answers.unity3d.com/questions/598492/how-do-you-set-an-order-for-2d-colliders-that-over.html but I do not want a huge work-around involving Raycast/Linecast.
I appreciate your work-arounds, but Clicking a gameobject that is on top, is the most basic concept in computer gaming and I have hard believing that Unity does not have a straight forward answer to this.
Answer by DientesDeCheto · Jan 07, 2016 at 06:34 PM
Are you shure, that your Objects have different z-axis when game is running? (Maybe z-axis is overwritten) Do you use 2d BoxCollider? Try with 3d BoxCollider! When Object B's BoxCollider is setted off, does Object A's BoxCollider trigger?
I have several GameObjects flying through the Screen, each of them need to be adjusted on z-axis automatically depending on which was clicked, and without any problems I have following Components with different configs: -Transform-Position: Z-Axis = -1, -1.1, -1.2, etc and -2 to bring those to the front (top) -BoxCollider (not BoxCollider 2D!!!) -RigidBody
Thats all I have attached. I remember I had some issue with BoxCollider 2D, so I decided to use BoxCollider. I hope it was usefull. Good Luck @isteffy
Your answer
Follow this Question
Related Questions
Text popup onMouseDown 2 Answers
Why my OnMouseDown is lost?!!! 0 Answers
Can't click gameobject when over another trigger? 1 Answer
OnMouseOver Problem 3 Answers
Raycast examples needed - alternative to OnMouseDown, OnMouseUp 1 Answer