Instantiate on mouse position

Hello everyone,

what i’m trying to do here is to Instantiate a prefab by moving the mouse to the right place and click. then it just Instantiated. i succeed to make it appear but the problem is the wrong position. the mouse coordinates are just so wrong. to understand the problem here’s a ![picture
][1] to what i’m trying to accomplish . and the code the i used is this:

#pragma strict
var ThePrefab : GameObject;


function Update () {

	var mousePos = Input.mousePosition;
	print(mousePos.x + " " + mousePos.y);

	
	if(Input.GetKeyDown("q")){
		var instance : GameObject = Instantiate(ThePrefab, Vector3(mousePos.x,mousePos.y,0), transform.rotation);
	}
}

Mouse coordinates are in screen space, and you want to instantiate in world space. To convert between the two using your main camera:

var worldPoint : Vector3 = camera.ScreenToWorldPoint(
    Vector3(mousePos.x, mousePos.y, camera.nearClipPlane));

Use worldPoint to instantiate your prefab.