- Home /
instantiate object at mouse position
Hello
Could anyone please explain me how i have to instantiate a prefab at the current mouse position? I already know how to instantiate prefabs but what i don't know is how to instantiate them at the currrent mouse position.
Thank you.
Answer by Edy · May 21, 2011 at 10:56 AM
Get the current mouse position with Input.mousePosition, then convert that position to world space using Camera.ScreenToWorldPoint.
EDIT: Example in C#:
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 2.0f; // we want 2m away from the camera position
Vector3 objectPos = Camera.current.ScreenToWorldPoint(mousePos);
Instantiate(yourObject, objectPos, Quaternion.identity);
}
}
How do i have to do that? I've never worked with Input.mousePosition before? Could you please explain it a little more in detail?
I have keep getting an error. I posted the error as an answer because i couldn't attach my script in a comment :D
If I use the above I get an error. If I change Camera.current to Camera.main, my prefab will spawn. However, my player is a god, watching the world from above (3D game), and the prefab is spawned at the gods position. How can I get the prefab to spawn down on the ground?
Answer by BarkShark · May 21, 2011 at 11:41 AM
I keep getting a error when i press the mouse:
NullReferenceException UnityEngine.Camera.ScreenToWorldPoint (Vector3 position) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/Graphics.cs:650)
Do you have any idea what this is?
This is my sript:
var testObject : Transform;
function Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
var mousePos = Input.mousePosition;
mousePos.z = 2.0; // we want 2m away from the camera position
var objectPos = Camera.current.ScreenToWorldPoint(mousePos);
var myObject= Instantiate(testObject, objectPos, Quaternion.identity);
}
}
Try using Camera.main ins$$anonymous$$d of Camera.current. If the error persists, include a reference to your main camera in the script.
im not too great at unity but i had to do something similar for a 2D game, shouldnt u maybe create the variable objectPos as a camera object by putting var objectPos : Camera, before the Update function and set it in unity to ur main camera.
Answer by Maverickthebest · Jan 06, 2019 at 06:02 AM
mousePos.z = 2.0; is this not float?? i mean: mousePos.z = 2.0f;
The code is unityscript, not C#. Floats don't use the -f suffix here.
yes sir it does matter and it is c# and they use -f suffix
Answer by SystemSuitable · Apr 13, 2020 at 10:16 PM
For anyone attempting to do this in C# is quite simple & works quite fine as of 4/13/20...
private Vector3 mousePos;
private Vector3 objectPos;
public GameObject yourPrefab;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
mousePos = Input.mousePosition;
mousePos.z = 2.0f;
objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(yourPrefab, objectPos, Quaternion.identity);
}
}
}
i have something simmilar but i cant get it to appear on the game screen only on the scene screen can you help me
Your answer
![](https://koobas.hobune.stream/wayback/20220612082137im_/https://answers.unity.com/themes/thub/images/avi.jpg)