Mouse Click and Spawn object

Hello,

i got this script, but it don't work as i need..

 var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

if ( Input.GetMouseButtonDown(0) ){
var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity);
}

}

it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:

You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?

"it don't work" aside from being an abomination of the English language is also not very explicative. It tells neither how it failed to work nor in what context.

Instantiate does not take a ray as a position. You should try:

Instantiate(bednaPrefab, ray.origin, Quaternion.identity);

or more than likely

Instantiate(bednaPrefab, hit.point, Quaternion.identity);

Hello sorry, but i haven't much time before...

now:

I want to spawn a crate - where i click with my mouse. I want only X a Y axis, Z will be 0 (side platformer game).

I want to click & then spawns a crate (in air) and then it falls.

So i attached that script to main camera & it gives me this error:

Assets/Scripts/qe.js(12,40): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Ray, UnityEngine.Quaternion)' was found.

I have rescripted it:

var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

if ( Input.GetMouseButtonDown(0) ){
var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity);
}

}

Now it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:

You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?

This is a very old question I know but maybe some one will find this helpful. This works for me...

Should work - attach to main camera

var bednaPrefab : Transform;

function Update () {

var mousex = Input.mousePosition.x;
var mousey = Input.mousePosition.y;
var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));

var hit : RaycastHit;

if (Physics.Raycast (ray, hit, 200)) {

}
if ( Input.GetMouseButtonDown(0) ){
var create = Instantiate(bednaPrefab, hit.point, Quaternion.identity);

}

}