x


iOS Instantiate transform as child -- positioning bug

I'm building an app where the user can customize bicycles on an iPad.. Currently I'm working on switching bicycle components based on a GUI touch.

I'm able to instantiate prefabs in the correct postion since I add them as a child to the bicycle gameObject, so if the user rotates the bicycle and adds a new gameObject it will update the position of where it needs to be instantiated.

The problem I have encountered is that the first time I instantiate a prefab, it is not added as a child, and has the Vector3(0,0,0). Instantiating the second time however the prefab is in the correct position, rotation and is added as a child to the bicycle gameObject... I'm really not sure what's going on here. Has anyone else encountered a similar problem? Possibly I'm overlooking something in my script?

var saddle : GUITexture;

var clone : Transform;  //new bicycle component to replace orig
var orig: Transform;   //original bicycle component used for copying position+rotation
var bike : Transform;   //parent transform

    function Update () {

    if (Input.touchCount>0)
       {
          for (var touch : Touch in Input.touches)
          {
         if(touch.phase == TouchPhase.Began && saddle.HitTest(touch.position))
             {       
            clone.transform.position = orig.transform.position;
            clone.transform.rotation = orig.transform.rotation;
            clone.parent = bike;

     clone = Instantiate(clone,transform.position,transform.rotation);
     print (clone.transform.position);
         Debug.Log("TouchSaddleBtn");  
             }
           }
         }
      }
more ▼

asked Jul 11 '12 at 05:23 AM

LANDO gravatar image

LANDO
54 10 13 18

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

2 answers: sort voted first

try: ...

if(touch.phase == TouchPhase.Began && saddle.HitTest(touch.position))
             {       

     clone = Instantiate(clone,orig.transform.position,orig.transform.rotation);
     clone.parent = bike;
     print (clone.transform.position);
         Debug.Log("TouchSaddleBtn");  
             }

...

since clone is a "permanent" var, the line "clone = instantiate..." sets it to the instantiation, but since your other commands are BEFORE this, it was simply null (since at this point "clone" does not exist inside the scene).. then the next time you tap, the data is applied to the object previously instantiated. (And immediately re-instantiated at the new position..)

also, is "clone = instantiate(clone..." intended? usually it would be a separate var, like:

var newClone = Instantiate(clone...

where clone is a reference to the prefab being instanced. In your code, you are replacing the var with it's copy.. not necessarily an issue though, I suppose..

more ▼

answered Jul 19 '12 at 08:01 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

Thank you Seth, this is working really well for me now. I totally overlooked the order of all of my commands which made the "clone" point null.

I didn't intend using "clone = instantiate(clone.." for any particular reason, it was the only way that made sense at the time of writing it.

Thanks again for all of your help, I'll post the updated code in an answer below.

Jul 19 '12 at 02:29 PM LANDO
(comments are locked)
10|3000 characters needed characters left

Updated code thanks to Seth Bergman:

if(touch.phase == TouchPhase.Began && saddle.HitTest(touch.position))
    {       
       var newClone = Instantiate(clone,orig.transform.position,orig.transform.rotation);
       newClone.parent = bike;
       print (clone.transform.position);
       Debug.Log("TouchSaddleBtn"); 
    }
more ▼

answered Jul 19 '12 at 02:33 PM

LANDO gravatar image

LANDO
54 10 13 18

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

x1953
x1668
x1274
x418
x407

asked: Jul 11 '12 at 05:23 AM

Seen: 397 times

Last Updated: Jul 19 '12 at 02:33 PM