- Home /
Best Practices: Instantiate Prefabs
I assumed that prefabs were a good way to design an entity in your game (i.e. set the model, materials, AI scripts, etc). Then you could reference that template in other scripts to create instances of the entity. However, based on further reading, it seems the only way to create prefab instances is via calls to Instantiate.
From the documentation, Instantiate only clones existing instances. When creating a new enemy, I would not want the state of a dead one. I want the original state defined in the template. Also, I would want any init (Awake() or Start()) scripting associated with the template to run when the new instance is created. Am I missing the point of prefabs and Instantiate?
What are best practices for defining an enemy template and using that template to stamp out instances via scription?
Answer by by0log1c · Sep 17, 2012 at 12:44 AM
Hmm, it really isn't too complicated.
In the Editor, your create your prefabs and spawns as many instances as you want just by dragging them in the scene, each of these instances will be untied when you hit Play and carry on with their own variables and stuff, leaving the prefab unnaffected.
Through code, whatever GameObject reference you pass as an argument will be used as the template for the cloned object, so you'll want to pass the original prefab instance and not an already existing object, which probably has been modified (ie: you could spawn an already dying character).
Just add a GameObject reference somewhere and drag your prefab in and use that with Instantiate, done.
If you have an instance of your prefab in the game, the Awake() and Start() methods have already been called, right? So even if I hide an instance of my prefab in the game somewhere and use that as an argument to Instantiate, I do not get the original prefab template. I get a copy of an existing instance and its state.
So you would say that you should not use the Awake() or Start() methods of prefab scripts to configure entities?
Woah, I've never claimed anything close to that. Awake and Start are very useful for initialization and they are called on any script that enters the world, whether they're visible or not.
I think you're overthinking the issue, you should do some testing to figure out how it behave. Just passing the original prefab to the Instantiate should be enough to just 'spawn a brand new enemy'. Its really as simple as adding a GameObject variable in a script and dragging the prefab in the Inspector. Alternatively, you could use Resources.Load but that's slightly more complicated.
I created a script like this: float timeUntilGeneration = 0f; public Transform gremlinPrefab; void Update () { timeUntilGeneration += Time.deltaTime; if (timeUntilGeneration > 5) { Transform gremlin = (Transform)Instantiate(gremlinPrefab); timeUntilGeneration = 0; } }
Then I dragged the prefab from the Project view to the Gremlin Prefab property in the inspector. It behaves as you described. Thanks.
Answer by Eric5h5 · Sep 17, 2012 at 01:12 AM
> From the documentation, Instantiate only clones existing instances.
No, it's used for instantiating prefabs from the project as well. That page you linked to describes the process in some detail. (Asset -> Create Prefab, drag into project view, etc. Although a nifty shortcut is to simply drag an object from the scene into the project view, then it automatically creates a prefab from that object.)
You are correct. I created a script that has a reference to the prefab. Then I dragged the prefab onto the property in the inspector. When calling Instantiate on it, it does create an instance from the script.
Thanks.
Your answer
Follow this Question
Related Questions
Prefab Sharing Textures 1 Answer
finding instantiated prefabs 2 Answers
Wrong coordinates of setting position of the object 1 Answer
Checking if object intersects? 1 Answer
how Load Prefabs after build 0 Answers