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 /
This question was closed Oct 11, 2018 at 07:41 PM by Lord_Grumpledorf for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Lord_Grumpledorf · Oct 11, 2018 at 07:04 PM · canvasimageuser interfacerecttransformdevelopment

Swapping UI image element positions

I have searched extensively for a solution to this problem and haven't been able to find one yet. I have the below script placed on an object I call the ability manager. My application is built in 2D.

 private Controls controls;
 public List<Canvas> abilityImageList = new List<Canvas>();
 public Vector2[] currentPos = new Vector2[3];
 public RectTransform current, up, down;


 // Use this for initialization
 void Start ()
 {
     controls = GameObject.Find("Settings").GetComponent<Controls>();
     abilityImageList.Add(GameObject.Find("Ability1Sorting").GetComponent<Canvas>());
     abilityImageList.Add(GameObject.Find("Ability2Sorting").GetComponent<Canvas>());
     abilityImageList.Add(GameObject.Find("Ability3Sorting").GetComponent<Canvas>());

     current = abilityImageList[0].GetComponentInChildren<RectTransform>();
     down = abilityImageList[1].GetComponentInChildren<RectTransform>();
     up = abilityImageList[2].GetComponentInChildren<RectTransform>();
 }

 // Update is called once per frame
 void Update ()
 { 
     if (Input.GetKey(controls.up))
     {
         currentPos[0] = current.posi;
         currentPos[1] = up.position;
         currentPos[2] = down.position;
         
     }
 }

alt text It finds the 3 canvas objects that I currently have, each with one image component (broken apart as such to allow changes to their order in layer). I'm hoping to make a sort of rotary ability selection menu. Currently, when I hit up (the key I have set to grab each images position) it returns the same X/Y value for each object. I've tried putting in localPosition and anchoredPosition with no luck. The returned values are always identical between the 3 objects.

Image for .position: alt text

The result is similar regardless of which method I use. The values might change, but they are identical between the 3 images. I'd like to know if there's some different way I haven't found yet to get the positional information for the 3 images. Any help is greatly appreciated!

position.png (15.9 kB)
hierarchy.png (10.1 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

  • Sort: 
avatar image
1
Best Answer

Answer by LCStark · Oct 11, 2018 at 07:25 PM

As the GetComponentInChildren documentation states:

Returns the component of Type type in the GameObject or any of its children using depth first search.

So you're probably retrieving the RectTransform of the Canvas instead of the Image. Try accessing the child first and then retrieving its component:

current = abilityImageList[0].transform.GetChild(0).GetComponent<RectTransform>();
or
current = abilityImageList[0].transform.Find("Ability1").GetComponent<RectTransform>();
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 Lord_Grumpledorf · Oct 11, 2018 at 07:41 PM 1
Share

You're totally right... I actually noticed that right before I came to check the page hoping nobody had seen it yet... Appreciate the quick response though!

Follow this Question

Answers Answers and Comments

101 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

Related Questions

How to align pixel art with UI system ? 0 Answers

Pointer Enter event doesn't fire Unity 5.5 1 Answer

Force UIImage to stay on viewport? 0 Answers

Moving images around 0 Answers

Universal Method of finding pixel dimensions of UI elements 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