• 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 Krzysiek_Mee · May 09, 2018 at 09:19 AM · transformlocalposition

Local position zero leads to lower left corner

I have a GameObject which is procedurally instantiated, and placed within a UI bar. This works fine. However, when I instantiate another kind of gameobject and set it to be the child of the object:

 private void PlaceObj(ObjDelegate Obj)
     {
         Obj.transform.SetParent(transform);
         Obj.transform.localPosition = Vector3.zero;
     }

The resulting object is placed in the lower left corner of the parent object. The anchors are all set to center. Why is the positioning off?

What is more, when I check in-game for the position (by a key press), it returns (0,0,0), but when looking in the inspector it has -x and -y values.

Edit: Posting a picture of the bug:

alt text

Edit: I managed to find the issue; It had something to do with anchoring, however, when I explicitly added:

 Obj.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;

it works. I don't know why the anchors said different things in editor and scripts.

bug2.png (364.5 kB)
Comment
Add comment · Show 3
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 HHammerite · May 09, 2018 at 09:50 AM 0
Share

Does the parent gameObject has a layout group of any kind?

avatar image Krzysiek_Mee HHammerite · May 09, 2018 at 10:03 AM 0
Share

No, no grouping present at all

avatar image tormentoarmagedoom · May 09, 2018 at 10:02 PM 0
Share

Good day.

Can you post some screenshots of inspector, and the GameObjects Scene?

Thanks :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HHammerite · May 09, 2018 at 08:57 PM

I had a similar issue today now that I think about it. It was a sprite that was instantitated as a child, anchors set to the center etc. The inspector showing values, and in the lower left of the parent. This was using 2018.1. I would say it's just a bug, I simply set the local position to Vector3.zero after setting the parent. I can see you added a code snipped above did resetting the position help in your case?

Comment
Add comment · Show 2 · 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 Krzysiek_Mee · May 09, 2018 at 09:53 PM 0
Share

Yeah, I added a drag&drop functionality to move the child around, and when dropped it is replaced in the main object. It is still placed at -x,-y, and the value of localPosition is still returned as 0,0,0.

avatar image Krzysiek_Mee Krzysiek_Mee · May 09, 2018 at 09:58 PM 0
Share

What is even funnier, I use the same code for moving around/placing other type of child and parent objects, and it works perfectly fine.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

105 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Code does not work 1 Answer

localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

Issue with moving character along his/her local z axis when space key is pressed. (Parkour) 1 Answer

Finding the forward vector of a child in local space? 3 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