using mouse to move an object

I have created an object in my scene, what exactly I wanna do is that, I want to click on my object using my left click (mouse) and the object moves towards a certain point, then clicking back again on the same object to return to it’s position. which method can I use to do this function and if possible can somebody include an example of a script or something, I am totally new to this unity game engine.

Add a collider to the object you wish to move. You should also put it on a layer that holds all movable objects.

Next you want to create a script that sends a raycast from the camera using something like this?

int layerMask = (1 << LAYER_FOR_MOVABLE_OBJECTS); 
RaycastHit hit = new RaycastHit ();
if(Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 1000, layerMask)) {
//Get hit.point.x, and hit.point.y, and hit.point.z to grab the position of where it hit
}

If you are making the game 2D or from a side view, you could dont have to raycast and you could just get the Input.MousePosition and move the object on 2 axis (like x and y) based on the mouse movement. Moving it around in the 3d world requires the code i just posted above, but you’re going to have to figure the rest out.

Your box needs a Box Collider and the following script:

var ObjectLayerMask : LayerMask;//Select the layer your box or whatever is in

var Position1 : Vector3;//leave empty, it gets filled automatically in Start()
var Position2 : Vector3;//enter the coordinates of your desired location to move towards

var MoveSpeed : float = 4;

var CurrentPos : int = 1;

function Update()
{
	var ray = Camera.main.camera.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	
	if(Input.GetButtonDown("Fire1"))
	{
		if (Physics.Raycast (ray, hit, Mathf.Infinity, ObjectLayerMask)) 
		{
			Move();
		}
	}
}



function Move()
{
	var StartPosition : Vector3 = transform.position;
	var EndPosition : Vector3;
	
	if(CurrentPos == 1)
	{
		EndPosition = Position2;
		CurrentPos = 2;
	}
	else
	{
		if(CurrentPos == 2)
		{
			EndPosition = Position1;
			CurrentPos = 1;
		}
	}
		
	
	
    var t : float = 0.0;
      
    while (t < 1.0) 
    {
	    t += Time.deltaTime * MoveSpeed;
	    transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
	    yield;
    }
}