x


copy settings from other gameobject

hi,

i got an game object with components & scripts attached to it. if the player clicks on a gui button the engine needs to destroy the current object and load a new object with the same component scripts settings position etc. (like a swap of objects)

this is what i got

var pos : Rect;
var pos2 : Rect;


function OnGUI()
{
  if (GUI.Button(pos, " Swap "))
  {
   Destroy (gameObject);


player = new GameObject ("the new gameobject");
player.AddComponent ("Rigidbody");
player.AddComponent ("SphereCollider");
player.AddComponent ("Mesh Renderer");
player.AddComponent ("Drag NRotate 2");
  }

greetz

more ▼

asked Jun 01 '10 at 10:02 AM

ab_cee gravatar image

ab_cee
35 10 10 14

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

2 answers: sort voted first

Perhaps you could just use Instantiate to create the new object, then destroy the old object. Eg:

var newObject = Instantiate(oldObject);
Destroy(oldObject);
more ▼

answered Jun 01 '10 at 10:38 AM

duck gravatar image

duck ♦♦
41k 92 148 415

IIRC Instantiate doesnt copy the renderer values so you might have to rehash those as well, all the times I have used it then tried to change the r,g,b,a values I got 'renderer is not attached to this object' where it was in the original.

Jul 03 '12 at 11:00 AM EdzUp[GD]
(comments are locked)
10|3000 characters needed characters left

If the settings are always the same, just instantiate a prefab. If not, then write a method which will copy all the values (like position, mass etc.) from one object to another.

more ▼

answered Jul 03 '12 at 12:56 PM

Wolin gravatar image

Wolin
61 2 3

(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:

x5077
x3460
x2086
x108
x51

asked: Jun 01 '10 at 10:02 AM

Seen: 3049 times

Last Updated: Jul 03 '12 at 12:56 PM