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?

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.