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
0
Question by Michael_Miller · Mar 31, 2015 at 10:49 AM · uiinstantiateprefabbuttonparent

Is there a way to Instantiate a button prefab as a child of a Canvas?

I'm trying to instantiate different buttons, depending on the character preset the player chooses in the menu screen.

I have this script attached to the Canvas gameobject in Unity:

 [SerializedField] GameObject spawnPoint;
 [SerializedField] Button [] abilityButton;
 
 void Start()
 {
   if(StaticVariableContainer.characterSelect == "Small")
   {
     Instantiate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transform.rotation);
   }
 }

Note: the spawnPoint object is just an empty object I placed in the position I wanted the button in so I had a transform to pass to the instantiated object.

Currently, this will spawn an instance of the button, but at the top of the heirarchy, as it's own independent gameobject with no parent. If I move it under the Canvas after it's been instantiated it explodes in size, filling up the screen.

Is there a way to Instantiate the button directly as a child of the Canvas? When messing around in Editor, dropping the prefab onto the Canvas object resulted in a functional button being placed right where I wanted it.

I've also tried passing in the Canvas' position and rotation, as well as not passing any transform data at all.

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
3
Best Answer

Answer by _joe_ · Mar 31, 2015 at 11:12 AM

Simply use transform.SetParent(transform):

 GameObject myButton = Instantiate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transform.rotation) as GameObject;
 
 myButton.transform.SetParent(yourCanvasVariable.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 Michael_Miller · Mar 31, 2015 at 04:22 PM 0
Share

I tried a similar method, something like:

 Instatniate (abilityButton[0], spawnPoint.transform.position, spawnPoint.transfrom.rotation);
 
 abilityButton[0].transform.parent = canvas.transform;

and it threw an error. Something about not being able to change the parent of a prefab to avoid data corruption.

avatar image _joe_ · Mar 31, 2015 at 05:02 PM 0
Share

.parent is deprecated, that's why I used .SetParent(transform) in my example.

avatar image Bonfire-Boy · Mar 31, 2015 at 05:07 PM 0
Share

@$$anonymous$$ichael_$$anonymous$$iller As signalled by the error message, with that code you're changing the parent of the prefab, not the clone. Whether using .parent or SetParent() you need to do what Joe does and get a reference to the clone then set its parent rather than the prefab's.

avatar image _joe_ · Mar 31, 2015 at 05:13 PM 0
Share

Exactly, I was on my phone i wasn't able to clearly read the code.

Simply store the Instantiated object in a GameObject (don't forget the "as GameObject" at the end to convert the Object into a GameObject) and then change its parent.

Be careful, you wrote Instatniate ins$$anonymous$$d of Instantiate.

avatar image Michael_Miller · Mar 31, 2015 at 06:44 PM 0
Share

Oh! Thank you @Bonfire Boy for clearing it up, and thank you @joe for the answer! I totally missed that you were referencing the specific game object. I appreciate you guys helping me figure this out.

Edit: Grammar

Show more comments
avatar image
0

Answer by AssoBaba · Aug 15, 2017 at 05:45 AM

I am working on 2D game, I have a issue here how I place button prefabs randomly moving in downward direction? I just build button prefabs now I want to spawn downward direction. Can you please help me to resolve the issue?

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

6 People are following this question.

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

Related Questions

Instantiate New Gun 1 Answer

positioning ui buttons in a scrollview 1 Answer

Pressing space calls the wrong function 1 Answer

How to change Image on button click in a prefab that is instantiated 1 Answer

Instantiate a Prefab as child 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