How to check if a specific instance of a prefab is pressed?

This one is hard to explain, so I'll show the code first -

for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);

if (hit)
{
clicksNeeded -= 1;
GetComponentInChildren<TextMesh>().text = "" + clicksNeeded;
Debug.Log(clicksNeeded);
print("was a hit");
}
}
}


I need to check if hit touches the specific game object that the script is linked to, as at the moment if any of the circles are clicked, clicksneeded -= 1 to all objects currently instantiated.

Thanks for the help!

A ray is not the right tool for this job. You want to check whether your circle collider contains the touched point, using OverlapPoint:

Camera camera = Camera.main;
float depth = Vector3.Dot(
camera.transform.forward,
transform.position - camera.transform.position
);

for (var i = 0; i < Input.touchCount; ++i)
{
var touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began)
{
Vector3 screenPoint = touch.position;
screenPoint.z = depth;

Vector3 worldPoint = camera.ScreenToWorldPoint(screenPoint);

if (myCollider.OverlapPoint(worldPoint))
HandleHit();
}
}


Or, if you want to take the thickness of the touch into consideration, you could compare the distance vs your collider/touch radius:

var worldDistance = Vector2.Distance(
worldPoint,
transform.TransformPoint(myCollider.offset)
);

HandleHit();

• if you don't have time to answer this, that's fine. This is an educational question because I'm a student and I really want to get into game dev at uni. But why are rays not the way to do this? – Nepthyz Robson Apr 20 '19 at 18:11
• A raycast detects where a ray enters the collidable surface of an object. In 2D, the collidable surfaces are the edges of the shapes - so a Physics2D.Raycast against a CircleCollider2D checks where the ray crosses the circumference of the circle. It won't capture a hit from a camera line of sight firing along the z axis and striking somewhere in the middle of the disc, because there is no z axis in the 2D physics world. If the ray didn't cross the circumference from outside to inside in the xy plane, then the 2D raycast will register no hit at all. – DMGregory Apr 20 '19 at 18:22
• myCollider 'does not exist in the current context'. – Nepthyz Robson Apr 20 '19 at 18:34
• Right, because you need to declare and assign a variable before you use it. So, do that. – DMGregory Apr 20 '19 at 18:46
• excellent - for other people having this issue i created a 'public CircleCollider2D myCollider' and put my circle collider inside of it. It works, thank you very much DMGregory. – Nepthyz Robson Apr 20 '19 at 18:49

in unity every gameobject has a unique instance id. so in instantiation moment you can get its id and check the id in raycast or any other event. its the code example:

string id =WantedGameObject.GetInstanceID().ToString();
if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
{
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
Debug.Log("Something Hit");
if (raycastHit.collider.name == id)
{
Debug.Log("wanted hit");
}

}
}