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
2
Question by adrianseeley · Jun 01, 2015 at 11:45 PM · uiprogrammatically

Bring UI Image To Front Programmatically

I have a UI image in the new UI system, it's the child of a canvas with several other UI elements (buttons, images, text). I have an image residing at the "back", and I want to move it to the "front" programmatically.

 Image img = GetComponent<Image>();
 img.z = 1000;

This code obviously doesn't work, and I'm having one hell of a time finding a solution that isn't "drag it lower in the hierarchy".

There should be a dead simple straight forward single function call answer, and I can't find it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by YoungDeveloper · Jun 01, 2015 at 11:52 PM

If your canvas is screen space - overlay, child elements are rendered from top to bottom, so lower elements are rendered last - on top of previous. Simply set it as last transform.

Comment
Add comment · Show 6 · 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 adrianseeley · Jun 03, 2015 at 12:35 PM 0
Share

Thanks that did the trick, I'm a little disappointed in Unity for not providing an index based ordering function when that is clearly the underlying structure.

avatar image YoungDeveloper · Jun 03, 2015 at 01:21 PM 1
Share

You can in fact do index manipulations with transform children. For example this will take last child and transfer it on the front lines.

 Transform lastchild = this.transform.GetChild(this.transform.childCount-1);
 lastchild.SetSiblingIndex(0);
avatar image Crevecoeur YoungDeveloper · Jun 07, 2016 at 07:47 AM 0
Share

Hi and sorry about digging up this post but is it possible to bring the first child of the UI to the front ? That's my example : alt text

This titles bar is made of 5 UI Images. In my Unity hierarchy, the first title is the first transform, etc... But the problem is about the "chevron" shape of this bar title because the first title image has to be in front of the second and the second title in front of the third, etc. With the Unity automatic ordering system I don't know how to fix that.

auto-colors-gone-nuts.png (2.6 kB)
avatar image Kutulue Crevecoeur · Jun 07, 2016 at 07:59 AM 0
Share

Hi Crevecoeur.

Elements are renderered from top to bottom. So whatever is first in your Unity editor UI will be rendered first. Items lower in the list will be rendered over the top of the higher up ones.

So in your case can you put the Dark grey chevron with "Well" text as the first item then go in reverse order.

Well

As

Nice

Are

Greys

Show more comments
avatar image
0

Answer by Kutulue · Mar 18, 2016 at 05:42 AM

I think you have the SetSiblingIndex variable wrong. SetSiblingIndex(0) will move the element to the top of the Heirachy which will put it behind all other elements. SetSiblingIndex(10000) will force it to the bottom as long as there are not more than 10000 elements in your heirachy. Being at the bottom of the Heirachy will force it to the front of the display.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

4.6 C# change canvas dependent on value 0 Answers

Unity 2021 LTS: IPointerEnterHandler is broken 1 Answer

DropDown menu(add item from input field): Object reference not set to instance of an object C# 1 Answer

Cannot accessing text in InputField Array 1 Answer

Changing the target of a button that takes a gameobject as a parameter? 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