
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 ?
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
}
}
}
if(hits == "panel1") ??
\$\endgroup\$
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\$
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.
Sprite Rendererin the inspector you can see aSorting layerand anOrder in Layerproperty. You can use those properties to identify if the object is on top or behind another object. This tutorial covers this topic. \$\endgroup\$