Clicking a gameobject help

I managed to get andeees script for clicking a game object and altered it to attempt to click a game object - this is the code that is attached to the object that holds the scripts to handle (almost) everything in my scene:

var target1: Transform;

function Update () {
	target1 = GetComponent(battleBeginSetup).player1Object.GetComponent(Transform);
	if (Input.GetMouseButton(0)) {
		var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit: RaycastHit;
		
		if (Physics.Raycast(ray, hit)) {
			if (hit.transform == target1) {
				print("Hit target 1");
			}
		} else {
			print("Hit nothing");
		}
	}
}

The thing is that the inspector shows during runtime that this object is set as target1, however the scene contains a player standing on a plane. If I click the plane it returns absolutely nothing. Yet if I click outside of the plain, even if I’m clicking the object (ie. player) in question - it returns ‘hit nothing’. Anyone able to help me? I’m almost tearing my hair out :frowning:

I have found the solution to your problem! In your “Physics.Raycast” if-statement you must say -

var collider = hit.collider;

if(collider.transform==target1)
{
	print("Hit target 1");
}

Or you could also use the built-in mouse functions. Add a script to the target1 game object saying-

//This function is called when the player has clicked the game object.
//You could also use OnMouseDown but, I suggest using OnMouseUp

function OnMouseUp ()
{
    print("Hit target 1");
}