- Home /
Change prefab before instantiation
Is it possible (or sensible) to change a "prefab" in a script before passing it to instantiate?
For example, something like the following:
// assigned in the editor var prefab : GameObject;
function Start() { prefab.GetComponent(FooComponent).ownerObject = this.gameObject; }
function Foo() { var newObject : GameObject = Instantiate(prefab, Vector3.zero, Quaternion.identity);
// now don't have to do this - a saving if instantiating many objects of this type
//newObject.GetComponent(FooComponent).ownerObject = this.gameObject;
}
I tried this with Network.Instantiate, and it broke horribly and silently (at which I'm not surprised). Now I'm wondering if this would work with the standard Instantiate function?
I have actually never tried to do so, but why not change it after you have initialised it?
Well, if this is a script in my player object that fires bullets, I was hoping to set the owner of every bullet instantiated by this player by altering the prefab once for each player, ins$$anonymous$$d of setting it after every instantiation of a bullet on every player.
Answer by d3coy · Jan 02, 2011 at 06:42 PM
It does work, but it has the consequence that the altered Prefab will remain changed when you leave play mode. You're literally altering the Prefab, so the changes persist beyond runtime.
Ok, thanks. It's interesting to know that unity still regards it as a prefab rather than a GameObject as its type indicates. Oh well.