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 /
  • Help Room /
avatar image
0
Question by Chloedan · May 11, 2017 at 03:08 PM · instantiateprefabsparent

How to set lots of Instantiate prefab Gameobjects in the same parent?

alt text

I want to let be cubepivot0(Clone) as child of "長條圖"

I've tried some solution but it never worked

     GameObject temp=Instantiate(cube, transform.position, transform.rotation) as GameObject;
                 temp.transform.SetParent(bar);

or

     GameObject temp=Instantiate(cube,transform.position, transform.rotation) as GameObject;
     temp.transform.parent=bar;

or

 Instantiate(cube, transform.position, transform.rotation) ;
  
 GameObject.Find("cubepivot0(Clone)").transform.SetParent( bar);

but it failed

1.png (8.5 kB)
Comment
Add comment · Show 4
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 Matthewj866 · May 11, 2017 at 04:07 PM 1
Share

Have you checked bar is actually assigned an object?

avatar image Chloedan Matthewj866 · May 11, 2017 at 04:13 PM 0
Share

I declared bar as Transform

avatar image Matthewj866 Chloedan · May 11, 2017 at 04:15 PM 1
Share

The transform of what though?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AhsanNaeem · May 11, 2017 at 06:41 PM

Try this one:

     [SerializeField] GameObject buttonPrefab;
     [SerializeField] Transform pp;
     GameObject button = (GameObject)Instantiate (buttonPrefab);
     button.transform.parent = pp;

Here pp is the parent. Button is created from Prefab and it becomes child of pp.

Comment
Add comment · Show 3 · 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 AhsanNaeem · May 11, 2017 at 06:46 PM 0
Share

If you want to create more GameObjects for the same parent you can do it in for loop like:

 for (int j = 0; j < 10; j++) {            
     GameObject button = (GameObject)Instantiate (buttonPrefab);
     button.GetComponentInChildren<Text> ().text = (j + 1).ToString ();
     button.transform.parent = pp;
 }

avatar image Chloedan AhsanNaeem · May 12, 2017 at 11:07 AM 0
Share

This method didn't work for me.....

SO I tried this code below here

 for (int i = 0; i < num; i++)
         {
 
             Instantiate(cube, new Vector3(i,i,i), transform.rotation);
 
             GameObject.Find("cubepivot0(Clone)").transform.SetParent(GameObject.Find("長條圖").transform);
             GameObject.Find("cubepivot0(Clone)").transform.localPosition = new Vector3(-6 + (6 / (num - 1)) * (i),-3, 0);
             GameObject.Find("cubepivot0(Clone)").transform.localScale = new Vector3(1, 0, 1);
 }

And it works.....but ONLY for one clone Why can't all of them get in the same gameobject? alt text

1.png (17.6 kB)
avatar image AhsanNaeem · May 12, 2017 at 05:02 AM 0
Share

If your parent is not declared as Transform then you can use:

 button.transform.parent = pp.transform;

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

117 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 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

Destroy a prefab from another class 2 Answers

How to get the position of a new Instantiated prefab 0 Answers

Rotation of instantiated object 1 Answer

How can I in-script create gameobject from prefab? 2 Answers

Random Obstacle spawn (Infinite Runner) 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