how to detect mouse click on a gameobject.

I have an assignment and I am lacking idea in this regard. I am surely a new bee and eagerly waiting for the help.

Super Collapse Puzzle Gallery 2 - Free Online Games | GameFools

Please see the game

By looking at this game I got two ideas, one is to use cubes(with rigidbody and colliders attached) and other is to use the GuiTexture. If using cubes, I am think that how to detect the mouse click on individual cube and if using GuiTexture than how to know whether something is below or not.

Please help me

As far as the cube method goes you can use a simple mouse over function and ignore the raycasting entirely. look something like…

function OnMouseOver(){
   if(Input.GetMouseDown(0){
      // Whatever you want it to do.
   }
}

That is for JS though the method could easily be ported to C# as well.

You can use the below script to identify the mouse click on particular game object.

void Update()
 {
        //Check for mouse click 
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit raycastHit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out raycastHit, 100f))
            {
                if (raycastHit.transform != null)
                {
                   //Our custom method. 
                    CurrentClickedGameObject(raycastHit.transform.gameObject);
                }
            }
        }
 }

public void CurrentClickedGameObject(GameObject gameObject)
{
    if(gameObject.tag=="something")
    {
    }
}

void OnMouseUp()
{
/Do whatever here as per your need/
}

As well as you can use 
 
void OnMouseDown()
{
  /*Do your stuff here*/
}

USe this link to get more info regarding mouse related function---->link text

If found useful then don’t forget to mark the answer…

This will check each frame if there is something under the mouse. Then it checks if it is a cube (via tag). If so you could manipulate the cube using hit.collider.gameObject (such as changing it’s forward/backward position, size, material, etc.):

function Update(){
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     if(Physics.Raycast(ray,hit)){
          if(hit.collider.tag == "clickableCube"{
               //hit.collider.gameObject now refers to the 
               //cube under the mouse cursor if present
          }
     }
}

As for the GUITexture, you can just use that in the OnGUI function to replace the mouse cursor:

var crosshair : Texture;
function Start(){
     Screen.showCursor = false;
}
function OnGUI(){
     var pos = Input.mousePosition;
     GUI.DrawTexture(Rect(pos.x-crosshair.width/2,pos.y-crosshair.height/2,crosshair.width,crosshair.height),crosshair);
}

Updated script for this which should work okay:

if (Input.GetMouseButtonDown (0)) {    
			var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit, 100)) {
				// whatever tag you are looking for on your game object
				if(hit.collider.tag == "Trigger") {     					
					Debug.Log("---> Hit: ");    					
				}
			}    
		}

function OnMouseDown(){

	this.gameObject.renderer.material.mainTexture = texture;//guiTexture = texture;

}

If someone is still facing the issues so you can check this article https://gamedevsolutions.com/detect-mouse-click-on-gameobject/

Hope it will be helpful for someone, Let me know if you are facing any issues we can discuss that here or in the blog comment section.