Run script when object is clicked

This seems like a really simple problem but I’m having real difficulty finding the answer.

I have a simple script attached to a series of objects, each is a slight variation on the other, and I need these scripts to launch when the object is clicked on by a mouse cursor.

So for example I have objects A, B and C with corresponding scripts A, B, and C.

How do I get a mouse click on object B to launch script B attached to it?

Hi and welcome to Unity Answers :slight_smile: I would raycast from the camera. like so:

// This holds the info on what we hit
RaycastHit hit;
// find your camera and convert your mouse position to a ray
Ray ray = Camera.current.ScreenPointToRay(Input.mousePosition);
// rayCast into the scene And require that something is hit has a transform
if (Physics.Raycast(ray, out hit, Mathf.Infinity) && hit.transform != null)
{
    // try to retrieve classA from the script
    ClassA tmp = hit.GetComponent<ClassA>();

    // did we hit something with the script on it? 
    if (tmp != null)
    {
        // if so.. Fire the missiles! 
        tmp.LaunchScript();
    }
}