x


Instantiate problem

i want to swap objects using instantiate.

im having trouble using instantiate, could someone help me with the code.

  var pos : Rect;
    var newObj = Instantiate("The new 3d Object"); 

    function OnGUI()
    {
      if (GUI.RepeatButton(pos, "Bodyconditionscore 2"))
      {
      newObj.transform.parent = oldObj.transform.parent; 
      Destroy("My old 3d object");
      }
    }

greetz,

more ▼

asked Jun 08 '10 at 12:11 PM

ab_cee gravatar image

ab_cee
35 10 10 14

What is the outcome of your code? Where is oldObj defined? Does oldObj has a parent?

Jun 08 '10 at 12:18 PM Extrakun

the old obj is in the gamescene and needs te be destroyed completely: the parent with children etc. and a new object needs te be exactly in the same spot where the old object was

Jun 15 '10 at 11:21 PM ab_cee
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

When you instantiate with just a prefab it'll appear at the location that you saved it in (could be anywhere). When you use transform.parent it changes the parenting of the object but it wont move to where the old object was.

What you need to do is instantiate the new object at the same transform.position (or alter it's position afterwards, which ever you prefer).

At the moment I'm assuming your old object gets destroyed and then you can't find the new object because it appeared else where.

Do something like this:

function OnGUI()
{
   if (GUI.RepeatButton(pos, "Bodyconditionscore 2"))
   {
      var newObj = Instantiate(objPrefab, oldObj.transform.position , oldObj.transform.rotation); 
      newObj.transform.parent = oldObj.transform.parent;
      Destroy(oldObj);
   }
}
more ▼

answered Jun 08 '10 at 01:22 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

i tried this code but do i need to create some new variables. ??? cuz im getting some errors?

Jun 15 '10 at 11:24 PM ab_cee

Yes, you need to add your own header with all the variables declared. objPrefab is type GameObject where you drag the prefab onto in the spector. oldObj is the thing you're replacing.

Jun 16 '10 at 03:00 AM spinaljack
(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:

x3458
x3328
x2086
x1672
x51

asked: Jun 08 '10 at 12:11 PM

Seen: 1406 times

Last Updated: May 27 '11 at 04:04 AM