1
\$\begingroup\$

I’m making an Editor tool -- a ScriptableWizard -- that instantiates a prefab on a grid. But I find that when I update the prefab, the instances don’t update.

When I instantiate prefabs interactively in the Editor by dragging them from the Project panel to the Scene or Hierarchy, then updates to the prefab affect the instances as expected.

I see another difference between my scripted instances and the ones created when I drag the prefab into the scene: when I do it interactively, the instance in the hierarchy is listed in blue. But the instances I create from script using the same prefab are listed in black, similar to objects created directly in the scene.

Here’s the hierarchy; the “JitterPrefab” was dragged into the scene, and the ones under “gridParent” were instances of the JitterPrefab generated from my script:

dragged vs generated instances in scene

Here’s how I’m generating the instances:

public GameObject prefab; ...

void OnWizardCreate() { ...
  Transform instance = Instantiate (prefab.transform, pos, rot, parent.transform);
  instance.name = string.Format ("L{0:D2}-R{1:D2}-C{2:D2}", l, r, c);
  ... 
}

When I make a change on the prefab, e.g. change the radius of the Jitter script from 0.025 to 0.25, the manually created instance updates:

change prefab... ...dragged instance updates

But the script-generated instances do not:

...scripted instances don't update

What am I doing wrong? Why are my scripted instances no longer connected to the original prefab? And, separately, how to I tell in the editor (or in script) whether an object in the scene is an instance of a prefab or not?

\$\endgroup\$
5
\$\begingroup\$

Instantiate creates it as a game object thus loosing the prefab link. You want to use PrefabUtility.InstantiatePrefab() in order to spawn the object in the scene and maintain the prefab link.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.