- Home /
How do you set a Prefab through javascript?
var waypointPrefab : Prefab;
//i just want to set it here...
function Start () {
waypointPrefab = ...(users/***/Prefabs/e.c.t);
}
Answer by aldonaletto · Oct 01, 2011 at 07:42 PM
You usually set the prefabs at the Inspector. You can also read objects from the Resources folder and assign them to prefabs - or even instantiate directly from the resource read. Take a look at Resources.Load - the docs have some useful examples.
cool, looks like Resources.Load will help. thanx It is set in the inspector but i am trying to make it so all i need to do is drop my unit.js on any unit and it will function. And also to clean up the inspector so it holds only what is needed, bit easier on the eye like, OCD really ;)
But the Resources folder have some disadvantages:
Unity will include all stuff in those folders even if you don't use it
In Webbuilds you can't control when those files are loaded. Unity will load them even before the first scene to make sure all is available. This can increase the loading time.
The access is a bit slower since Unity have to find the object via path-string ins$$anonymous$$d of using a direct reference.
If you select your script file in your project-panel (not a GameObject in the hierachy, the script file itself) you can setup the default-references. Those references can only be set to objects from the project-panel. It's not possible to assign something from the current scene (aka hierachy-panel). If you assigned a default reference Unity will automatically assign this reference when you attach this script to a GameObject. Already attached instances are not affected. It's just a little help to simplify the setup-process for your scripts so you don't need to assign the prefabs manually all the time.