• 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
1
Question by Uniqeum · Mar 15, 2021 at 11:50 PM · transform.position

object transform position not working

I use to set position of the object, but it appears in the center of the screen, not in the aim coordinates.

 newObject.GetComponent().anchoredPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);
   
 newObject.transform.localPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);
 newObject.transform.TransformPoint(new Vector3(targetPosition.x, targetPosition.y, floatz));
 
  rectTransform = newObject.AddComponent();
     rectTransform.position = new Vector3(targetPosition.x, targetPosition.y, floatz);
     transform.GetComponent<RectTransform>().localPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);
     image.transform.position = new Vector3(targetPosition.x, targetPosition.y, floatz);
     image.rectTransform.localPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);
     
     newObject.GetComponent<RectTransform>().transform.position = new Vector3(targetPosition.x, targetPosition.y, floatz);
    image.rectTransform.anchoredPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);
    newObject.GetComponent().localPosition = new Vector3(targetPosition.x, targetPosition.y, floatz);

None of the above ways work good - the image is always in the center of the screen (targetPosition isn't the center, to make this clear). How to make it right?

Comment
Add comment · Show 2
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 DevManuel · Mar 16, 2021 at 09:35 AM 0
Share

I don't know why it shouldn't work, but did you tried any debug? Maybe just let you print the targetPosition and the gameObject position to see if they are the same.

avatar image Uniqeum DevManuel · Mar 16, 2021 at 11:37 AM 0
Share

positions values are match (the same). What is strange, that in the same scene, there is the object, that scripted to move towards targetPosition, and he moves perfectly into target coordinates. Now i changed the script: i dont create any object or image in script, just create only image in unity inspector on the canvas. And then try to transform it's pos in script. But the image appears in the left bottom corner of the screen for some reason.

             image.rectTransform.position = new Vector3(targetPosition.x, targetPosition.y, floatz);
             Debug.Log(targetPosition.x);
             Debug.Log(targetPosition.y);

Idk, how it is working - no logic at all.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexc847 · Mar 17, 2021 at 03:30 AM

transform.position = targetPosition is the way to go I think, if targetPosition is a Vector3 value.

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 Uniqeum · Mar 17, 2021 at 09:06 AM 0
Share

It doesn't work too. May be the problem is in that the script, all above code is running is attached to another game object

avatar image alexc847 Uniqeum · Mar 21, 2021 at 01:09 AM 0
Share

Perhaps you should debug. Like maybe put the above code on a different new gameobject and set targetposition to a Vector3 value and see if it works.

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

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

Related Questions

Calculating where to position instantiated clone 1 Answer

How to make the camera acquire the position of a gameobject and start following it? 1 Answer

Recalculate linerenderer when moving gameobjects 1 Answer

Instanitiate PreFabs and let them move to different locations 1 Answer

smoothly change position of an Object 1 Answer


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