If I drag&drop a prefab onto a gameobject in the hierarchy while holding the alt-key, the gameobject will be replaced with an instance of that prefab, while maintaining its name and transform. How can I do this in script?
2 Answers
2http://www.unifycommunity.com/wiki/index.php?title=ReplaceSelection does something like this
leave the blank gameobject as a parent to the object you intend to replace.
when you wish to change it, have your script destroy the child object then:
instantiate the replacement,
parent
local rotation and local position set to "0"
and you're done
The way that script works is by instantiating a new copy of the prefab, copying over the name and transform, and then destroying the object. Unfortunately, this won't work for me, as other objects are referencing the object to be replaced. Additionally, under some circumstances the prefab will be switched while the object is actively being moved, and even DestroyImmediate() won't destroy an object while it's being moved.
– sneftelHence 'something like' ;)
– DaveAThis may be helpful: http://unity3d.com/support/documentation/ScriptReference/EditorUtility.ReplacePrefab.html
– DaveA