0
\$\begingroup\$

I am setting up a grid of tiles (2d sprites) in Unity with this code:

public class Grid : MonoBehaviour {

public GameObject slot;
public int gridW = 9;
public int gridH = 9;

private GameObject [,] grid = new GameObject[9, 9]; 

void Awake(){

    for(int y=0; y<gridW; y++)
    {
        for(int x=0; x<gridH; x++)
        {
            GameObject gridSlot = (GameObject)Instantiate(slot, new Vector3(x, y, 0), Quaternion.identity);
            gridSlot.transform.parent = transform;
            grid[x,y]=gridSlot;
        }
    }

}

I then would like to have printed on the console the x,y coordinates of an individual tile when clicked on it. This is the script attached to the tile prefab:

public class Tile : MonoBehaviour {

void Start () {


}

void Update () {

    if(Input.GetMouseButtonDown(0))
        Debug.Log(transform.position.x + " " + transform.position.y);
}

Yet when I click on a tile I get printed the coordinates for ALL of the tiles on the grid. Why is this happening? Is there something very basic I am missing...?

Thanks!

\$\endgroup\$
2
\$\begingroup\$

Yet when I click on a tile I get printed the coordinates for ALL of the tiles on the grid. Why is this happening? Is there something very basic I am missing...?

It appears as though you are spawning one Tile for every tile on your map. So an 8x8 map would have 64 Tile instances. Every instance is Update'd by the Unity runtime. When the mouse is down, every instance checks if Input.GetMouseButtonDown(0) is true, and when it is, prints its own position.

So every time you click, every tile prints its position.

You probably want to either check if the clicked position is actually within the bounds of the tile (the simplest-possible-solution) or have something else at a higher level, possible the map object, handle the mouse click, determine which tile was clicked, and tell that specific tile to do whatever needs to be done in response to a click.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html works if your tiles have colliders. Though, I'd use some kind of global method and get rid of the colliders. \$\endgroup\$
    – House
    Aug 5 '16 at 19:43
  • \$\begingroup\$ So Input.GetMouseButtonDown(0) is not specific to the single script that calls it but it's a 'global' event? Am I understanding this correctly? @Byte56 what kind of global method are you thinking of..? Thanks guys!!! \$\endgroup\$
    – Uknowho
    Aug 5 '16 at 22:26
  • \$\begingroup\$ GetMouseButtonDown is triggered when an object's collider is clicked, specific to the object the script is attached to. A global event would be a single script that detects clicks, determines where they are in relation to your tiles, then alters the tile at that location. About as specific as I can get in a comment. \$\endgroup\$
    – House
    Aug 5 '16 at 22:33
  • 1
    \$\begingroup\$ I think there's a typo in @Byte56's comment above. The description there applies to the OnMouseDown message, not the method Input.GetMouseButtonDown. The latter simply asks "is the mouse button pressed?" with no context about what scene objects are under the pointer. \$\endgroup\$
    – DMGregory
    Aug 6 '16 at 3:31
  • \$\begingroup\$ Yes, @DMGregory is correct \$\endgroup\$
    – House
    Aug 6 '16 at 3:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .