Unity Editor: Place objects by raycast

Editor script setup: how to place objects in the editor by raycasting


  • Download the two files and place them at the following location

Assets/Scripts/EditorHelper/ObjectGroup.cs :

  • Create any prefab (as much as you like) and place them in Assets/Prefabs/ (Needs to exactly named like this)
  • Create an empty game object and place the ObjectGroup script on it.
  • Click the enable editing button and right click/alt+left click on ground plane/other geometry



Just an empty file..

public class ObjectGroup : MonoBehaviour
    void Start() {}


public class ObjectGroupInspector : Editor
void OnSceneGUI()
        // If we are in edit mode and the user clicks (right click, middle click or alt+left click)
        if (__editMode)
            if (Event.current.type == EventType.MouseUp)
                // Shoot a ray from the mouse position into the world
                Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                RaycastHit hitInfo;
                // Shoot this ray. check in a distance of 10000.
                if (Physics.Raycast(worldRay, out hitInfo, 10000))
                    // Load the current prefab
                    string path = "Assets/Prefabs/" + __typeStrings[__currentType] + ".prefab";
                    GameObject anchor_point = Resources.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
                    // Instance this prefab
                    GameObject prefab_instance = PrefabUtility.InstantiatePrefab(anchor_point) as GameObject;
                    // Place the prefab at correct position (position of the hit).
                    prefab_instance.transform.position = hitInfo.point;
                    prefab_instance.transform.parent = __objectGroup.transform;
                    // Mark the instance as dirty because we like dirty
            // Mark the event as used
        } // End if __editMode
    } // End OnSceneGUI

6 thoughts on “Unity Editor: Place objects by raycast

  1. John says:


    It spawns the prefab(s) when I click on the scene view, but it doesn’t draw/render them until I deselect ‘Enable Editing’. Is this some new quirk in Unity 5? Thanks.

    • Jos says:

      Haven’t tested this in the new unity yet. This was not happening. You can probably add a line in the code to force an update. Will have a look if I find some time.

  2. […] I’m trying to use this fellow’s editor extension to place prefabs on a map: http://josbalcaen.com/unity-editor-place-objects-by-raycast/ […]

  3. Fran says:

    Thanks. It works perfect and the script is very abstract.

  4. Andrew says:

    Thank you for this code and the instructions. But it seems like there should be a page 2 or something, because this isn’t enough to make it work. What do I do with the ObjectGroupInspector script after placing it in the editor folder? What do I attach it to?

    • Jos says:

      The ObjectGroupInspector script is picked up automatically. I wrote an extra step to make it more clear. Let me know if you have any problems.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>