Instantiating game object, and the behavior of scripts attached to it

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;

You should never have a constructor in your MonoBehaviour classes. Unity does not call it the way you think I fear.

I advise you :

  • To get rid of your static count static variable

  • Add event / callback to your HexHandler class

  • Use a List in your Map to know how many hexagonal tiles you have created.

    public class HexHandler : MonoBehaviour
    {
    // Our coordinates in the map
    private int u;
    private int v;
    private System.Action onClickCallback;

      public int U
      {
          get { return u; }
          set { u = value; }
      }
    
      public int V
      {
          get { return v; }
          set { v = value; }
      }
    
      private void OnMouseUp()
      {
          if( onClickCallback != null )
              onClickCallback.Invoke( this );
      }
      
      public void Initialize( float x, float y, System.Action<HexHandler> onClick )
      {
          U = x ;
          V = y;
          onClickCallback = onClick;
      } 
    

    }


// Map class

private System.Collections.Generic.List<HexHandler> tiles = new System.Collections.Generic.List<HexHandler>() ;

// ....

HexHandler tile = ((GameObject)Instantiate(hexPrefab, new Vector3(xPos, 0, y * zOffset), Quaternion.identity)).GetComponent<HexHandler>();                

// Make sure the hex is aware of its place on the map
tile.Initialize( x, y, OnTileClicked ) ;
tiles.Add( tile ) ;

// ...

private void OnTileClicked( HexHandler tile )
{
     Debug.Log("Clicked on hex coord x : " + tile.U + ", y : " + tile.V);
     Debug.Log("total # = " + tiles.Count);
}