How do I modify this script so that instead of creating a standard cube, it instead creates a prefab, which can be specified from within the inspector? For example, instead of creating a cube when the button is clicked, it creates a building, or a tree, or a sherbet lemon.
var distance = 5;
function OnGUI ()
{
if (GUI.Button(Rect(25,25,100,30), "Place Block"))
{
var newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
newCube.transform.position = transform.position + transform.forward * distance;
newCube.transform.rotation = transform.rotation;
}
}
Try adding a variable at the top “var prefab : Transform” and replacing the whole newCube section with something like:
var newPrefab=Instantiate(prefab,transform.position+transform.forward*distance,transform.rotation);
That will set position and rotation of the prefab at the same time, also.
Take a look at Instantiate.