Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Feb 23, 2016 at 04:35 PM by 7Tomus for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by 7Tomus · Feb 23, 2016 at 03:00 PM · ui

How to move UI element inside of canvas programmatically

I am trying to move element in UI canvas programatically but to no avail.

Here is my setup: http://i.imgur.com/H8JjfYS.jpg

Here is the code with which im trying to move it to the middle bottom of the screen:

 Hp0 = GameObject.Find("Hp0").GetComponent<RectTransform>();
 Hp0.anchorMax.Set(0.5f,0f);Hp0.anchorMin.Set(0.5f,0f);

this is called in a method that is part of Start() Does anyone know how to do this?

UPDATE: thing is that the Unity's Set method in anchorMax / Min does not work. It needs to be set like this:

 Hp0.anchorMax = new Vector2(0.5f,0f); Hp0.anchorMin = new Vector2(0.5f,0f);

With anchorMax/Min you can set the anchor relative to the canvas size where the values range from 0 to 1. In this case new Vector2(0.5f,0f) is the bottom middle of the screen, new Vector2(0f,0f) would be bottom left of the screen etc.

From this point you can use

 Hp0.anchoredPosition = new Vector2(0,0);

which sets position in pixels relative to the anchor you set before. The new Vector2(0,0) means that the UI element you are moving will be positioned in the center of the anchor. If you use new Vector2(53,20) it will be positioned 53 pixels to the right and 20 pixels to the top from the anchor. You can use negative values here to go towards left side / bottom from the anchor.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

59 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

Related Questions

How to tell if a RectTransform is within the visible area of a ScrollRect 1 Answer

null reference exception with text 0 Answers

Why do my UI buttons keep breaking? 1 Answer

Can't update score text - small error breaks the game (Space Shooter Tutorial) 1 Answer

How to create a difficulty slider/setting for a unity game in a setting scene 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