- Home /
swap objects with instantiate
hi
im still having trouble with swapping my objects.
im using this code to spawn a new 3d object(the spawning works with a prefab)
var pos : Rect; var prefab : Transform;
function OnGUI() { if (GUI.RepeatButton(pos, "Bodyconditionscore 2")) { var newObj = Instantiate(objPrefab, oldObj.transform.position , oldObj.transform.rotation); newObj.transform.parent = oldObj.transform.parent; } }
now i want to destroy my old 3dobject. The new 3dobject needs to be spawned at the same spot as the old 3d object was.(just like a swap)
The scripts from the old 3d obj needs te be copied to the new 3d object.
I asked some people about this on the holy internet but i cant get this instantiate working. could someone help me with this. im not a pro @ javascript or scripting. but i know a little bit
Answer by Tetrad · Jun 15, 2010 at 11:42 PM
Any reason you can't just spawn two objects, have them share a parent that moves around, and just enable one/disable the other when you need them to swap?
oke i need a parent with all the scripts attached to it. how can i switch between these children? ab_cee 0 secs ago
Answer by DaveA · Jan 13, 2011 at 11:16 PM
The scripts are instantiated with the new prefab. I think you mean the variables that go with that script (the 'settings')?? If so, you should assign them across maybe something like this:
newObj.GetComponent(myScript).someSetting = oldObj.GetComponent(myScript).someSetting;
newObj.GetComponent(myScript2).someThing = oldObj.GetComponent(myScript2).someThing;
and then destroy the old object (or at least deactivate it)
Your answer
Follow this Question
Related Questions
Instantiate problem 1 Answer
How to correctly convert Object to GameObject 1 Answer
How can I instantiate a GameObject directly into an array? 3 Answers
Bullet Instantiation 1 Answer