• 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
0
Question by Utochka · Jan 15, 2015 at 05:27 AM · panel

Add prefab to panel in script

I have a canvas, in canvas I have a panel, in the script i want to add element to the panel. That's the code, which doesn't work for me

    public GameObject b;
    public Canvas canvas;
 
     void CreateA() {
           RectTransform panel = canvas.GetComponentInChildren<RectTransform>(); 
           GameObject a = (GameObject)Instantiate(b); 
           a.transform.SetParent(panel.transform, false);
     }

In the end it adds element to the canvas, not the panel.

How can to add it to the panel?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Jan 16, 2015 at 11:15 AM

 RectTransform panel = canvas.GetComponentInChildren<RectTransform>(); 

will get the RectTransform of ANY child, not necessarily the panel you want. if you know the name of the panel then search for it by name

 var panel = GameObject.Find("PanelNameHere");
 if (panel != null)  // make sure you actually found it!
 {
     GameObject a = (GameObject)Instantiate(b); 
     a.transform.SetParent(panel.transform, false);
 }

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 daniloescobar · Dec 27, 2016 at 12:23 AM 0
Share

Thank you!!$$anonymous$$an you save me!

avatar image
0

Answer by garrettstrobel · Jul 31, 2015 at 03:29 PM

Actually I believe what is happening is GetComponentInChildren does not function exactly how you would think. It starts with itself and looks for RectTransforms there, so you are in fact finding the RectTransform of the canvas not the RectTransform of the Panel.

I know this seems backwards and have wondered why unity does this, but the object looking for components in child seems to include itself in that search, go figure.

If you did GetComponent*s*InChildren and took the second element in the array [1], this should be your Panels rectTransform

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

Answer by cboxgo · Aug 19, 2015 at 06:08 PM

Why not just have:

 public GameObject b;
 public Transform panel;
  
 void CreateA() {
        GameObject a = (GameObject)Instantiate(b); 
        a.transform.SetParent(panel.transform, false);
 }


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 cboxgo · Aug 19, 2015 at 05:40 PM 0
Share

p.s. this is not tested code, just my thought based on your question.

avatar image lamphung719 · Dec 27, 2016 at 02:47 AM 0
Share

this surely creates no bug

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way to handle a scenario game with a LOT of panels? 0 Answers

triangles at the corner of canvas panels not showing. what do I do ? 1 Answer

Panel GameObject not activating when called from another script 1 Answer

Unity split screen for Mesh preview 1 Answer

[4.6] Adding scroll to this combo? 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