How to create .blend models by script


I’d really like to know how to add my model from a .blend file into the scene, programmatically.

If you were to create a cube by script, it would be something like :

 `GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); `

Is there a way to do the same thing with .blend models?

Sure… Here’s how you do it:

  1. Bring your Blender model into Unity

  2. Drag your model onto the hierarchy. This turns it into a GameObject. Here you can adjust materials, add other behaviors, etc. like any other GameObject.

  3. Drag your item from the Hierarchy back into the Project window. This creates a prefab (Basically a save file of that GameObject). Now that it’s saved, you can delete the one in the hierarchy.

  4. Create a script, something like this:

    public GameObject MyPrefabToSpawn;

    private void Awake() {
    var myInstance = Instantiate(MyPrefabToSpawn);

  5. Attach the script like any other, drag/drop your prefab into the “MyPrefabToSpawn” field, and hit Run.