- Home /
Question by
RealMTG · Dec 31, 2014 at 01:05 AM ·
uiinstantiate
Instantiate object on mouse click and when cursor IS NOT on UI
Hi!
I am working on some kind of Level Editor and I got a small problem. When you click on the UI to select the current object it instantiates the object anyways. How do I prevent this?
This is currently my code:
if(Physics.Raycast(ray, out hit, levelEditorSettings.placementRange, levelEditorSettings.placementLayer))
{
hitPos = new Vector3(hit.point.x, hit.point.y, hit.point.z);
var temp = hitPos;
if(levelEditorSettings.enableGrid)
{
temp.x = Mathf.Round (temp.x / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
temp.y = Mathf.Round (temp.y / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
temp.z = Mathf.Round (temp.z / levelEditorSettings.gridSize) * levelEditorSettings.gridSize;
hitPos = temp;
}
if(Input.GetMouseButtonDown (0))
{
SpawnObject(spawnThisObject, hitPos);
}
if(editorMode == 0)
{
DrawCurosr(spawnThisObject, hitPos);
}
}
Comment
Best Answer
Answer by Oliver1135 · Dec 31, 2014 at 01:08 AM
I had this problem earlier,
use EventSystem.current.IsPointerOverGameObject() to find if the mouse is over a UI element in which case you may want to return or do not instantiate
This works. You can also implement a physics raycaster ins$$anonymous$$d of your own input code. See here for details.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175234im_/https://answers.unity.com/themes/thub/images/avi.jpg)