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 Lizozom · Dec 02, 2012 at 10:38 PM · instantiatetransformpositioninstanceglobal

Instance child global position

I am instantiating a prefab in different locations. This prefab contains some children objects. I can retrieve the position of the instances (as parents), but when I try to get the position of the children they always stay the same. How can I retrieve the correct global position of an instance of a prefab?

ScreenShot1:


ScreenShot2:

---------------------------------------------Fixed-------------------------------------------

I was using GameObject.Find().transform.position to get the positions of the children. But GameObject.Find() does a global search and because the children were belonging to instances of the same prefab they had the same name and my GameObject.Find() would return me always the same object(the child in the first instance).

However, I found out that GameObject.transform.Find() does a local search instead and given the correct path it returns the transform for the right child.

I ended up getting the positions of the children like this:

 var prefab:GameObject; //This is my prefab, a section of the floor with some obstacles
 function Start () {

     //The first instance of the section placed at the origin 
     var instance1:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 0.0f), transform.rotation);

     //The second instance of the section placed at an offset
     var instance2:GameObject = Instantiate(prefab, Vector3(0.0f, 0.0f, 20.0f), transform.rotation);

     //Getting the position of the child "O1" of the first instance
     var pos1:Vector3 = instance1.transform.Find("obstacles").Find("O1").transform.position;

     //Getting the position of the child "O1" of the second instance
     var pos2:Vector3 = instance2.transform.Find("obstacles").Find("O1").transform.position;

     //Printing the positions of the children
     Debug.Log(pos1);
     Debug.Log(pos2);
 
     //Printing the positions of the parents
     Debug.Log(instance1.transform.position);
     Debug.Log(instance2.transform.position);
 }

And the section prefab is structured like this:

alt text

cap.png (3.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Caiuse · Dec 02, 2012 at 10:56 PM

`transform.position` returns the global position of any object regardless of its parenting. If the prefabs children's local positions are (0,0,0), then printing the position `print( child.transform.position );` would print the same as its parents transform.position.

On the other hand if you want the local position for the child object relative to it's parent, use this `transform.localPosition`

Comment
Add comment · Show 5 · 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 Lizozom · Dec 03, 2012 at 12:12 AM 0
Share

That's what I thought but for some reason it seems it's not this way.. one second.. I will post some screenshots

avatar image Lizozom · Dec 03, 2012 at 12:18 AM 0
Share

Just added some screenshots. This is what I see in the inspector and I get the same in code. You can obviously see that the positions of the gizmos are different but the values are the same.. how is that possible?

avatar image Caiuse · Dec 03, 2012 at 09:47 AM 0
Share

The inspector only shows the local position, there may be a way to change that but I'm not aware of it. Print the objects position by making a little script function Start(){ print(YourTransform.position); } . I'm assu$$anonymous$$g that you only need the objects global position for program$$anonymous$$g purposes... in that case just using transform.position will work for you.

avatar image Caiuse · Dec 03, 2012 at 09:49 AM 0
Share

OR this answer gives you a very handy little editor extension script

avatar image Lizozom · Dec 03, 2012 at 06:44 PM 0
Share

Thank you very much for the comment. Everything you say makes sense. But the whole problem started from getting the same values in code. I use transform.position as Caius suggests and I get the same values for the positions of two objects which are clearly in different positions in space.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

How can I get a GameObject's transform in game and set that to a variable? 1 Answer

Use GameObject's global position instead of local position 3 Answers

How do I get the opposite position of an object that spins around another object... 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