-1
\$\begingroup\$

ScreenShot From Unity 4.6

I have made my own game, but I can't change the object's collider properties from the interface. I can't click on the expand button, because behind it there are other colliders.

How could I change the collider's properties ?

\$\endgroup\$
6
  • \$\begingroup\$ @Shiro no my friend, I mean both is same, but different obeject. If i click button on object1, button on object2 is not clicked too ? how can I fix it ? \$\endgroup\$ Commented Jul 8, 2015 at 23:11
  • \$\begingroup\$ @Shiro maybe my problems like making pop up in unity2D, so if the pop up is appear (How can I click button on pop up, but button behind the pop up is not clicked too) \$\endgroup\$ Commented Jul 8, 2015 at 23:21
  • 1
    \$\begingroup\$ In your Sprite Renderer in the inspector you can see a Sorting layer and an Order in Layer property. You can use those properties to identify if the object is on top or behind another object. This tutorial covers this topic. \$\endgroup\$ Commented Jul 8, 2015 at 23:27
  • \$\begingroup\$ @Shiro i know that, but this about it collider. I already using order in layer too, but I don't know to different it collider. \$\endgroup\$ Commented Jul 9, 2015 at 4:45
  • \$\begingroup\$ Maybe he means detecting clicks on overlapping objects? When you have this problem, is your game running or not? \$\endgroup\$ Commented Jul 9, 2015 at 6:23

2 Answers 2

0
\$\begingroup\$

I suppose you are using Raycasting to detect mouse clicks. You can use Object's name or Sorting Layer or Order in Layer or a combination of those properties to identify which layer you hit. There is no standard way to do what you are trying to do. It is just preference.

You can attach this script to your camera, or background or an empty object you made specifically for this script.

// In your Update method
void Update()
{
    // If left mouse was clicked
    if (Input.GetMouseButtonDown(0))
    {
        Vector3 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        // Use raycasting to determine which objects were clicked
        RaycastHit2D[] hits = Physics2D.LinecastAll(clickPosition, clickPosition);
        // If any object's 2D-collider was clicked
        if (hits != null)
            // This will iterate through ALL clicked objects
            foreach (RaycastHit2D hit in hits)
            {
                // The clicked object
                GameObject clickedObject = hit.collider.gameObject;
                // The clicked object's sprite renderer
                SpriteRenderer renderer = clickedObject.GetComponent<SpriteRenderer>();

                // Print a few properties of the clicked object
                Debug.Log(clickedObject.name); // Object's name                    
                Debug.Log(renderer.sortingLayerName); // Sorting Layer                    
                Debug.Log(renderer.sortingOrder); // Order in Layer
            }
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ yeah i'll try this. In selection "hits != null" can I fill with gameobject's name ? -- Like if(hits == "panel1") ?? \$\endgroup\$ Commented Jul 9, 2015 at 15:24
  • \$\begingroup\$ What different if i use OnMouseDown() ? \$\endgroup\$ Commented Jul 9, 2015 at 15:38
  • \$\begingroup\$ @DoniPutra As far as I know OnMouseDown works only for the specific object which has a collider. The code I gave you abode should be attached on your camera or your background or an empty object you created just for this script specifically. Not necessarily the object that has the collider. \$\endgroup\$ Commented Jul 9, 2015 at 15:43
0
\$\begingroup\$

It is not very clear what you're asking. If it is about rendering objects in desired order, then @Shiro clarified that. If you're asking about working in the editor, then, create temporary layer called "temp". Assign this to the desired GameObject. Then, at the top of inspector, you see a layers drop down. Here you will see some options with an eye symbol. This determines visibility. Click on symbol for "Nothing". Now everything in your scene disappears. Next, click on the symbol in front of temp. Now, only objects belonging to the layer "temp" are visible.

\$\endgroup\$
2
  • \$\begingroup\$ How can I call this function on script ? \$\endgroup\$ Commented Jul 9, 2015 at 4:59
  • \$\begingroup\$ Not sure why you want to do it. If you are working in the inspector manually, you won't need to do that with a script. But, if you want you can use Camera culling mask. answers.unity3d.com/questions/561274/… \$\endgroup\$ Commented Jul 9, 2015 at 7:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.