Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to select top most sprite in a stack of sprites?

Discussion in '2D' started by TooEZGames, Feb 23, 2020.

  1. TooEZGames

    TooEZGames

    Joined:
    Jun 18, 2019
    Posts:
    4
    I'm stuck trying to figure out how I can select the top most GameObject in a stack of GameObjects. In the picture below, I have a Green and Yellow circle stacked on top of each other. If I try to select the top most gameobject (the green one), it selects the yellow circle. Is there a way that I can make sure that when I go to select a circle, it selects the top most one? I'm currently using the OnMouseDown() function built into unity.
     

    Attached Files:

  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You could deactivate the collider on all of the objects that are behind objects. Unless you want the player to be able to touch the bottom objects. If that's the case you could cast a ray from mouse point and detect all objects in the path of the ray and determine which object was touched first.
     
  3. TooEZGames

    TooEZGames

    Joined:
    Jun 18, 2019
    Posts:
    4
    I feel like this wouldn't be the way to go about it. I feel like deactivating the coliders below would make it more complicated than it needs to be.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Would actually be very easy. You just don't know how to do it. But okay, here's another EASY idea. Put your objects into an array and determine which one is on top. Insure the click accesses that top object.
     
unityunity