- Home /
Scripting - How to instantiate a bunch of models?
I have successfully imported a bunch of .fbx models into my Unity hierarchy and I was wondering if there's any easy to write a script (which will ultimately be attached to an empty game object) that will automatically instantiate the models at coordinates 0, 0, 0.
They are not in the Resources folder (since I've read from the Unity documentation that it is inefficient) so I can't use Resources.Load(...). If this is the only way, then I guess I can use it but I was just wondering if there's any other way.
Thanks for any help in advance.
Forgot to mention that I did look through some similar questions and all I could find were scripts pertaining to prefabs or those that required using the editor to manually associate models with variables.
What I'm interested in is a method to loop through the models in the hierarchy and spawn one copy of it in game at a certain location.
Answer by Eric5h5 · Apr 27, 2011 at 01:39 AM
By "hierarchy" do you mean the project view? If you've already dragged the models into the hierarchy then you don't need to do anything else. Is this something you want to do at runtime or in the editor? The question is pretty unclear. Anyway, not sure if this is what you mean, but you can make prefabs out of the models, and use Instantiate.
var models : GameObject[];
function Start() { for (model in models) { Instantiate (model, Vector3.zero, Quaternion.identity); } }
Ah, sorry; yes, I meant "Project View" when I said "Hierarchy".
I'm just looking for a quick way to use a script to replace the tedious task of manually dragging every model into the scene.
Answer by Bunny83 · Apr 27, 2011 at 01:41 AM
Actually you have to understand how Unity works. Unity only include those assets into the build that are used somewhere (referenced by a script variable or any other component). The final decision what will be included depends on what levels you include. The levels contains some of your predefined objects that may refer to other prefabs so they are included in that level.
Assets that aren't referenced somewhere will not be included in the build. If you want do use an asset without the need of manually setup the reference in the inspector you have to put those assets into the Ressources
folder. Now you can use Ressources.Load
to get the prefab reference dynamically at runtime. The downside is that everything that is inside the Ressources folder is included and for streamed webbuilds all those assets get loaded with the first level.
Just a sidenote: imported models (.fbx) are also prefabs but a special kind. These prefabs can't changed inside Unity. You can only reimport them (which should happen automatically when you change the fbx file). The type of a prefab is given by the PrefabType and can't be changed.
Ah, thanks for the explanation. So, if I am understanding you correctly, .fbx models imported into Unity are automatically cast as a Prefab?
In that case, is there anyway to find a Prefab in the Project View by name or must it be manually linked to a variable via the inspector?
I realize that all of this can be bypass by simply putting the models in the "Resources" folder but that hardly seems like a optimal solution given what the Unity documentation says about using that method.
Did you read what i wrote? Assets that aren't referenced by something aren't included in your build! It wouldn't make a difference if you delete those files from your project because they aren't used. You can't change that behaviour. You only have those two possibilities: Have a manual like to all models you need (like Eric showed you) or put the files in the ressources folder. Only files in the ressources folder can be accessed by name.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
scripting question 3 Answers
Instantiated Particles reverse direction. Why? 1 Answer
Streaming Game Content 1 Answer