Tuesday, August 26, 2014

Unity Tutorial: Instantiating a Building on a Lot - JS

Here's a quick guide to how to Instantiate a Prefab to a specific position and rotation. The example I'll be using is calling a pre-made building (prefab) onto a lot, but the scripting operation can be used for any location-specific Instantiation.

______________________________________________________________

Object and Target

The key to this technique is to have a target-object that will represent the position and rotation you would like your object to take. For the sake of this tutorial, we'll use a Lot. This is a square with the scale dimensions of 40, 1, 40 and given a dirt material.

Model Street Lot

Now here's the script in Unity's very own JavaScript. This script has been tested and should work if you

Assign "lot" to your target Game Object in the scene

drag object from Hierarchy into "lot" slot in the Inspector

Store your Building prefab in your Resources folder

drag building object into Project to save it as a Prefab

set "resourceLoadString" as the path to your prefab in your Resources folder