- Home /
Question by
PretzelTail101 · May 14, 2019 at 09:16 PM ·
instantiatepositionmouse
Can't Instantiate an object at the clicked position.,I can't Instantiate an Object at the clicked position. I am trying to make a minecraft game.
Here is my code: { public GameObject block;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
var mousePos = Input.mousePosition;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(block, objectPos, Quaternion.identity);
}
}
}
I keep getting an error that says "NullReferenceException: Object reference not set to an instance of an object.
Comment
Answer by Kek_Kek · May 14, 2019 at 10:06 PM
You don't have a Game object assigned to block. The script should look like this
// Start is called before the first frame update
public GameObject block;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
var mousePos = Input.mousePosition;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(block, objectPos, Quaternion.identity);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612201416im_/https://answers.unity.com/themes/thub/images/avi.jpg)