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 /
avatar image
0
Question by Shubius · Jul 16, 2013 at 07:14 PM · instantiateorganizationinstantiating

What am I misunderstanding about Instantiation vs References to Prefabs?

After reading the Unity Manual regarding prefabs I think I may be misunderstanding prefabs vs prefab references. When an object exists in the scene (created procedurally or in the editor) which contains references to prefabs, are these prefabs instantiated automatically when the object is instantiated or is the reference a simple lightweight reference to the object on disc and not an actual instantiated version in memory?

I thought I was fine in my understanding until I read the following here: "When you add a Prefab to a scene, you create an instance of it."

Does "add a Prefab to a scene" include objects that have a variable which is a reference to a prefab?

Thanks in advance for any help.

Comment
Add comment · Show 2
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 SirGive · Jul 17, 2013 at 06:20 AM 0
Share

When you drag a prefab into the scene while not running to create a copy of said prefab, it maintains the reference to the prefab - in that - if you update the prefab, so will the instance in the scene. I believe, however, adding the prefab during runtime (through code or drag/drop) does not maintain the same reference, and will only be a snap shot copy of the prefab at that time. Though it isn't exactly practical to modify the prefab during run time.

avatar image SirGive · Jul 17, 2013 at 06:22 AM 0
Share

And "add a Prefab to a scene" would not include references that a script has to the prefab. Only after the prefab has been instantiated through code

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 17, 2013 at 06:12 AM

Say you had prefab A with a script with a variable that you initialized to prefab B. Instantiating prefab A does not create an instance of prefab B. All you have is a reference to prefab B that the script can use to Instantiate prefab B. So if you Instantiated prefab A with had this script:

 var goPrefab : GameObject;
 
 function Update () {

 if (Input.GetKeyDown(KeyCode.Space))
     Instantiate(goPrefab);
 }

No versions of 'goPrefab' would be created until the space bar is hit.

Comment
Add comment · 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
0

Answer by Shubius · Jul 18, 2013 at 07:03 PM

Thanks. This clears some things up for me.

Heres some background in case someone is having a similar problem:

I was working under the opposite assumption that prefabs that were referenced by an object were instantiated when the object was instantiated (not child prefabs but simply references). In my game i had a random loot list that returned the reference to an item prefab and i randomized the items properties and returned an item. I then instatiated the object thinking i was making a deep copy as the unity docs say is what instantiating a game object (as ipposed to a prefab) does. There was never an issue because i was always randomizing the items properties anyway. Upon inspecting my prefabs i noticed their values were not defaults but were randomized. I would have noticed the problem much sooner if i were not immediately instantiating the returned item as the next item randomized would have hanged the previous item's properties since it was still a prefab reference and i was actually randomizing the prefab properties.

Comment
Add comment · 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

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Instantiating thousands on start (loading custom levels) 3 Answers

Photon Cloud parenting instantiated gameobject 0 Answers

Instantiating a projectile pointing at target. 1 Answer

Instantiating on Input 1 Answer

Need help with instantiating a prefab in c# 2 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