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