- Home /
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.
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.
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
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110231im_/https://answers.unity.com/themes/thub/images/avi.jpg)