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,

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);
   }
}