- Home /
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");
}
}
}
}
Answer by Seth-Bergman · Jul 19, 2012 at 08:01 AM
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..
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.
Answer by LANDO · Jul 19, 2012 at 02:33 PM
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");
}
Your answer
Follow this Question
Related Questions
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
Instantiate Terrain Object as child of Empty Game Object 1 Answer
Getting instance of an sub object rather than the original's subobject 0 Answers
Make a simple tree 1 Answer
Instantiate a Prefab as child 0 Answers