- Home /
Instantiate without breaking prefab
Every time I instantiate a prefab, the "link" to the prefab is broken. Assuming I'd still want to modify the prefab and have the instantiated objects in a scene be updated accordingly, how can I instantiate a prefab without breaking its connection?
Instantiated Objects are not prefabs by definition. Changing the properties of an instance will not alter the other instances of the same prefab unless you are changing Static variables, which are shared across all instances of a particular prefab.
Populating them in the scene does not break the link, though you will be able to change the non-static variables without this propagating to the other instances, and this is intentional. The only changes that you want propagating to all of your instances are changes made to the prefab itself, either directly in the project panel, or by Apply To Prefab in the instances Inspector window.
You can't change the prefab at runtime, so if you are meaning to make changes to all objects of a particular type at runtime, Static variables are what you will want to look at. If you are just wanting to make changes to scene objects in the editor, use the Apply To Prefab option, or edit the prefab directly in the Project window
I understand your point, but I think you got a wrong impression of what I'm trying to accomplish - my bad, I'll try to be more specific. I'm working on a level editor in Unity that works through editor scripts. Those scripts instantiate objects under certain conditions/inputs in the editor, not at runtime. Since they're calling Instantiate(), though, those instances aren't created as prefabs, and they behave as you said.
However, if you drag and drop a prefab on your scene, it will still be linked to the prefab, so you get the effects of being able to apply, or change the original, and have all of its copies changed. I'm basically trying to achieve this effect, the same effect of "dragging and dropping" a prefab on a scene, without the "dragging and dropping" part, since I want to do it through code, through editor scripts.
So I guess my question should be, is there any way to simulate "dragging and dropping" a prefab on your scene - without losing its "link" - through code?
Ah, ok, so you are making an editor script to populate objects in your scene through code - not- at runtime. Understanding what you are after I can say with confidence that I'm not really sure how to do that, converting this to a comment so other know you still need help, and I'll keep investigating and will let you know if I find anything
Exactly, thanks for trying to help, the dude down there got it :)
Answer by Deni35 · Jul 17, 2013 at 05:25 PM
PrefabUtility.InstantiatePrefab
Thank you so much! This is exactly what I was looking for :)
Yeap, it works. @danielmlima , if it works for you check this answer as the correct.
Answer by Raimi · Sep 25, 2014 at 01:51 PM
The answer by Deni35 is perfect. Thank you! Just posting a little bit of code as an example...
//C#
GameObject clone = PrefabUtility.InstantiatePrefab(tile) as GameObject;
clone.transform.position = gridPositions[col,row];
clone.transform.localScale = new Vector3(cellSize,cellSize,cellSize);
clone.name = col.ToString()+","+row.ToString();
clone.transform.parent = t.transform;
Your answer
Follow this Question
Related Questions
Instantiate: Create Connection 1 Answer
How to break a prefab connection via c# 0 Answers
Prefab connection problem 1 Answer
Breaking prefab connection 1 Answer
instantiating vertically 2 Answers