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 darthbator · Nov 28, 2014 at 10:09 AM · uiinstantiate4.6

Instantiating UI elements in 4.6

Hey all!

I seem to be having an issue with button position and scale when I programmatically instantiate buttons on my menu. I'm getting a list of servers and then instantiating a button for each server. However when I child the button to it's parent container after instantiating it the button disapears into a thing red line as shown below.

alt text

Here's the code that instantiates and places that button.

         foreach (HostData host in hostData) {
             //Canvas.Instantiate(metaButton);
             Button button = Instantiate(metaButton, new Vector2(s_buttonFather.position.x, s_buttonFather.position.y + buttonOffset), Quaternion.Euler(Vector3.zero)) as Button;
 //            button.transform.SetParent(GameObject.Find("Canvas").transform, false);
             button.transform.SetParent(s_buttonFather.transform, true);
             button.GetComponent<ServerButton>().hostData = host;
             button.SendMessage("setName");
 
             buttonOffset += button.image.sprite.bounds.size.y + .7f;
         }


If I change the code above to not relocate the child object when I set the parent then the object is displayed out of the display view of the canvas camera as shown below. What's up with this? Am I making some glaring error that I am just somehow missing? Thanks so much!

alt text

screen shot 2014-11-28 at 2.05.39 am.png (21.3 kB)
screen shot 2014-11-28 at 2.08.40 am.png (22.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

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by malkere · Apr 23, 2015 at 10:55 AM

This was driving me nuts, but the manual says:

"When setting the parent of the instantiated UI element, it’s recommended to do it using the Transform.SetParent method with the worldPositionStays parameter set to false."

this one setting fixed all my problems.

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 aegodbold · Jun 24, 2015 at 11:03 PM 2
Share

Thank you so much!! This was driving me crazy, too!

avatar image
0

Answer by BOT_Sam. · Jan 26, 2015 at 11:17 AM

http://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

^^ Hope this helps ^^

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

28 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

Related Questions

Infinite ScrollRect? 1 Answer

How to stop non-rectangular buttons from overlapping? 4 Answers

Getting on screen sizes of a UI element in Unity 4.6 (while using CanvasScaler),Get the real size on screen of an UI element in Unity 4 0 Answers

4.6 UI Dynamic button event system PointerEnter, PointerExit, PointerUp etc. 1 Answer

Not instantiating prefab in the right place after 2017 update 2 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