Unity 5 - 2d Instantiate at mouse position


I’ve looked through as many search results as i could find, but there is nothing about Unity 5 2D instantiate at mouse position. The problem is that in my editor Camera.ScreenToWorldPoint only comes up with errors saying ‘Camera’ does not contain a definition for ‘ScreenToWorldPoint’. When I use the code below it instantiates the block VERY far away to the right:

if (Input.GetMouseButtonDown(1)) {
var block1 = Instantiate(block, new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z), Quaternion.identity);

Ok, I got lucky and Unity fixed the obsolete version of the code:



Vector3 objPosition = GetComponent().ScreenToWorldPoint(mousePosition);