x


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

more ▼

asked Jun 15 '10 at 11:38 PM

ab_cee gravatar image

ab_cee
35 10 10 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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)

more ▼

answered Jan 13 '11 at 11:16 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Jun 15 '10 at 11:42 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

oke i need a parent with all the scripts attached to it. how can i switch between these children? ab_cee 0 secs ago

Jun 15 '10 at 11:50 PM ab_cee
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3442
x2070
x1667
x51

asked: Jun 15 '10 at 11:38 PM

Seen: 1640 times

Last Updated: Jun 15 '10 at 11:38 PM