Instantiate Asset from Project -- Inside Code (Completley)?

I’m new to Unity, but I am evaluating it for a very specific use at my company.

In my playing around I’ve run into a little problem that I can’t quite figure out. Hopefully the good people on the forums can help me out.

I want a script to instantiate a prefab. The prefab exists inside of my project but not inside the Hierarchy.

I can achieve this by doing pretty much what’s in the tutorials and making the script like this:

var obj : Transform;
function OnMouseDown () {
Instantiate(obj, Vector3(0,0,0), Quaternion.identity);
}

… then attaching the script to an object in the scene and setting through the editor the value of obj to the asset that I want to instantiate.

However…

I need to do something different! I need to be able to instantiate an asset completely programatically from inside the script, without using any external variables or setting anything through a drop-down menu. Here’s an example of what I want to do:

var arrayOfAssetName = ("this", "that");
function OnMouseDown() {
for (var assetName in arrayOfAssetName) {
var obj = functionIHaventFoundYet(assetName);
Instantiate(obj, Vector3(0,0,0), Quaternion.identity);
}
}

So…

is this possible? Is there any way to do a thing like this?

Can someone help me fill in my functionIHaventFoundYet? Or is there another way to do it?

Many Thanks!
N

If you really must do this, there is the Resources class:

But I’d just like to say I’ve made plenty of large projects with Unity without ever using this, so I hope you’re sure there wouldn’t be an easier way. :slight_smile:

Cheers,
-Jon

I used to use the Resources feature, but it can be risky and needs a custom build script.
Having said that, I haven’t found a perfect way to organize and load a large number of assets at runtime.

Jonathan - what kind of technique are you using for your large projects?

Cheers
Shaun

Thanks guys! That did the trick! (well, gets me halfway)

Is there a way to load a resource (like “Import Package…”) from a file dynamically?

Cheers,
N