- Home /
Instantiate a prefabe in scene view from menu!
Hi,
How can i instantiate a prefabe in scene view from a menu that i created in mouse click position in scene view?
means that first i want click on a menu(is created by me) and select a sub menu then when i click in scene view,the prefabe instatiate in mouse position that i clicked.
in addition i want to do that in design time no in realtime!
Please help, So thanks.
Answer by Julian-Glenn · Aug 12, 2010 at 01:23 PM
ScreenPointToRay can handle the instantiation of an object at your current mouse position.
Like in this example:
var Prefab : GameObject ;
function Update () { if (Input.GetMouseButtonDown(0)) { var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit;
if (Physics.Raycast (ray, hit))
{
var instance : GameObject = EditorUtility.InstantiatePrefab (Prefab);
instance.transform.position = hit.point;
}
}
}
And for the GUI stuff I'd suggest starting here:
http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html
Use the next buttons to navigate the entire section and once you've read that you should be able to handle the GUI menus easy peasy ;)
Update: Make sure to use EditorUtility.InstantiatePrefab in order to maintain the connection between the prefab and the new instance. With regular Instantiate you will get no connection, meaning changes to the prefab are not applied to the instances.
So thanks for respond,but i want to do that in design time no realtime!
Your answer
![](https://koobas.hobune.stream/wayback/20220613071034im_/https://answers.unity.com/themes/thub/images/avi.jpg)