Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Babagazeus · Sep 17, 2012 at 12:35 AM · instantiateprefabsbest practice

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Babagazeus · Sep 17, 2012 at 12:58 AM 0
Share

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?

avatar image by0log1c · Sep 17, 2012 at 01:06 AM 0
Share

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.

avatar image Babagazeus · Sep 17, 2012 at 01:34 AM 0
Share

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.

avatar image
0

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.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Babagazeus · Sep 17, 2012 at 01:35 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges