Skip to main content
added 741 characters in body
Source Link

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

EDIT: Alright I found that we need to use eventtype.MouseDown but now the script is not executing? there are no errors in the console window

using UnityEngine;
using UnityEditor;
[ExecuteInEditMode]
public class mapMaker : Editor
{
    public GameObject rock;
    private void OnSceneGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            Ray ray = Camera.current.ScreenPointToRay(Event.current.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                Debug.Log(Event.current.mousePosition);
                Vector3 newTilePosition = hit.point;
                Instantiate(rock, newTilePosition, Quaternion.identity);
            }
        }
    }
}

```

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

EDIT: Alright I found that we need to use eventtype.MouseDown but now the script is not executing? there are no errors in the console window

using UnityEngine;
using UnityEditor;
[ExecuteInEditMode]
public class mapMaker : Editor
{
    public GameObject rock;
    private void OnSceneGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            Ray ray = Camera.current.ScreenPointToRay(Event.current.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                Debug.Log(Event.current.mousePosition);
                Vector3 newTilePosition = hit.point;
                Instantiate(rock, newTilePosition, Quaternion.identity);
            }
        }
    }
}

```
added 144 characters in body
Source Link

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor.

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor. Currently I am trying to use input.GetMouseButtonDown but that doesn't seem to work ? Also is it fine to use raycasting in the editor itself?

grammar
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

I want to instantiate a prefab at the mouse position in the ediotr alleditor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate atin the editor.

I want to instantiate a prefab at the mouse position in the ediotr all the answers I found till now are for instatiate a prefab at runtime but i want to instantiate at editor

I want to instantiate a prefab at the mouse position in the editor. All the answers I found till now are for instatiate a prefab at runtime but i want to instantiate in the editor.

Source Link
Loading