This is a simple piece of code that I’m using in a hex-based map. Basically, each of the hex is attached with the following script:
public class HexHandler : MonoBehaviour {
// Our coordinates in the map
private int u;
private int v;
private static int count = 0;
public HexHandler(int u, int v)
{
this.u = u;
this.v = v;
count++;
}
public int U
{
get
{
return this.u;
}
set
{
u = value;
}
}
public int V
{
get
{
return this.v;
}
set
{
v = value;
}
}
private void OnMouseUp()
{
Debug.Log("Clicked on hex coord x : " + this.U + ", y : " + this.V);
Debug.Log("total # = " + count);
}
}
I’m instantiating these hex prefabs in the main ‘Map’ code, around ~40 of them. I’m getting a ‘0’ on all of the Debug.Log console outputs whenever I click on the objects. The instantiation part of the code is (using a for loop):
GameObject hex_go = (GameObject)Instantiate(hexPrefab, new Vector3(xPos, 0, y * zOffset), Quaternion.identity);
// Make sure the hex is aware of its place on the map
hex_go.GetComponent<HexHandler>().U = x;
hex_go.GetComponent<HexHandler>().V = y;