Just wondering how I can create a child for each generated hex and make that child a sphere collider? Thanks.

GameObject hex_go = new GameObject();
hex_go.name = “Hex_” + x + “_” + y;
hex_go.transform.position = new Vector3(xPos, y * yOffset, 0);
hex_go.transform.SetParent(this.transform);
hex_go.AddComponent();
hex_go.AddComponent < SphereCollider > ();

This script places a collider on the sprite itself but it forces me to name the entire sprite “Hex Collider” because i need the following script to work. Where as I only want to, in theory, name the collider so that the ray can find it.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo = Physics.RaycastAll(ray.origin, ray.direction);
foreach (RaycastHit h in hitInfo)
{
if (h.collider.name == “HexCollider”) //the name determines what happens when uncle ray ray strikes it.
{
//do something
Debug.Log(“Ray hit someone”);
}
}

Thanks Heaps!

Why not create a custom component instead, and check if the gameObject has that attached to it. It would probably prove more useful in the long run too.

@redphaze You could do it as follows:

GameObject hex_go = new GameObject();
hex_go.name = "Hex_" + x + "_" + y;
hex_go.transform.position = new Vector3(xPos, y, * yOffset, 0);
hex_go.transform.SetParent(this.transform);

GameObject collider_go = new GameObject();
collider_go.name = "HexCollider";
collider_go.transform.SetParent(hex_go.transform);
collider_go.AddComponent<SphereCollider>();

However, you should consider using a tag or a layer, rather than the object’s name, when choosing what to do, because comparing strings is a slower operation.

  1. If you just want to determine whether the hit object is a hex, use a tag.
  2. If you want the trace to ignore some things and not others, use a layer.