Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LANDO · Jul 11, 2012 at 05:23 AM · iosinstantiatetransformparentchild

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");  
              }
            }
          }
       }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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..

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LANDO · Jul 19, 2012 at 02:29 PM 0
Share

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.

avatar image
0

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"); 
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges