Dragging prefab onto object through code
I've been learning about Unity editor scripts lately, and I was wondering if there's a way to take an array of objects and turn all of them into instances of a given prefab. Basically, the code equivalent of what happens when you alt+drag a prefab onto an object.
I've tried adding prefabs to my scene via code, but they get added as new instances, whereas I'm trying to replace an existing object with a prefab. Is this possible?
Answer by Adam-Mechtley · Dec 02, 2016 at 08:13 AM
What exactly do you mean "they get added as new instances, whereas I'm trying to replace an existing object with a prefab"? Do you mean the instances are disconnected from the prefab? If that's the case, make sure you're using PrefabUtility.InstantiatePrefab instead of just Instantiate.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101843im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Editor script: Variable created on gameObject in editor set to missing on runtime 0 Answers
Why does GameObject.FindWithTag() get a wrong instance? 2 Answers
Editorscript: How to reference prefabs in editor not in awake or start 0 Answers
How can I detect prefabs edited in the prefab editor in OnValidate? 2 Answers