- Home /
Prefab Problem
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.)
I think you are confused between datatypes. Prefab is Gameobject. Which you can give reference to instantiate.
as @harschell said explain doubt with script.
Answer by Althaen · Nov 03, 2012 at 08:29 AM
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.
Answer by shavais · Nov 03, 2012 at 08:29 AM
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?
Answer by anilgautam · Nov 03, 2012 at 08:27 AM
You Can declare a gameobject in your script where you will drag and drop your prefab.
Sorry for digging an old thread, but since i struggled with something similar i thought to put down few words. What i noticed is that lots of people (me included) make prefabs, then delete them from the scene and put them into a "prefab" folder, as is explained in a few tutorials online. Then the object will get a reference with "GameObject.Find()", which won't give an immediate error. The problem is that when they try to instantiate them, the object can't be found because it's not in the scene but in the prefab folder, so it's not initialised. That can be solved by using the method above. Thank you for helping me getting to that conclusion (which i won't be surprised if it's explained somewhere in the unity API reference..).
Your answer
Follow this Question
Related Questions
how to control spawned prefab of player ANDROID 0 Answers
How to instantiate a prefab and change its color? 5 Answers
how do i stop instantiate from changing a prefab 0 Answers
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
Prefab instantiating problem 1 Answer