Im trying to instantiate a prefab, but the scripts in it require specific variables such as gameobjects and transforms, and none of those show up in the prefab, how can i save those set variables into the prefab along with the actual object. If you cant save them in a prefab, how can i set the variables to what i need them to be, (ex. GUITextures, GameObjects, Transforms, etc.)
First you need to connect an object to the script. In this example case a Rigidbody object. So save this in a variable.
//Drag the prefab with a rigidbody to the prefab& slot in the inspector
var Rigidbody : prefab;
function Update{
//Now instantiate it.
clone = Instantiate(prefab, transform.position, transform.rotation);
}
//transform.position instantiates the object at the coordinates you set.
//transform.rotation makes the object have the same rotation.
//You can place an empty object and attach this script to it to instantiate inside it but I'm sure there are other ways.
Say you have a “MissilePrefab”, and you have a “MissileScript” script assigned to that prefab, and MissileScript instantiates a “MissileExplosionPrefab” when the missile explodes. So MissileScript has a variable called “explosionPrefab” that needs to be set to MissileExplosionPrefab, so it can Instantiate the missile explosion when the time comes. But you can’t do that in the inspector, because MissilePrefab is a prefab in the project tree, not a pre-instantiated object in the scene.
What has worked for me is to create a “Resources” folder in the project tree (Resources folders can appear any number of times, anywhere under Assets), and move MissileExplosionPrefab into that folder, then in MissileScript in the Start() function, do this:
explosionPrefab = Resources.Load("ExplosionPrefab") as Transform; // or "as GameObject", if explosionPrefab is a GameObject.
Then the code that explodes the missile (in that script) can do this:
var explosion : Transform; // or GameObject
explosion = Instantiate(explosionPrefab, transform.position, transform.rotation); // instantiate the explosion at the position of the missile
gameObject.renderer.enabled = false; // or gameObject.Destroy(); -- hide the missile itself
I haven’t tried it with a GUITexture yet, but I’m pretty sure it’ll work for that, too.
Did I understand your question correctly?
You Can declare a gameobject in your script where you will drag and drop your prefab.