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 /
avatar image
0
Question by Mahunreah · Apr 14, 2018 at 08:34 PM · instantiateinstantiation

Instantiate as child of a to-be-instantiated list item

Hey =)

I have a bit of trouble transfering what I read in the previously asked questions about this topic in this discussion board to my own problem and would appreciate a bit of help.

I have a few lines of code that look like this:

 for (int i = 0 ; i < 3 ; i++)
 {
     kandidatenliste.Add (Instantiate(npcs.npc[i], KupplerinTransform));
     heiratskandidaten.Add (Instantiate(heiratskandidatPrefab, XXX ));
     heiratskandidaten[i].name = "Kandidat: " +npcs.npc[i].vorname +" "+ npcs.npc[i].nachname;
 }

"XXX" describes where my thought process takes a break. I want to instantiate "heiratskandidaten" as a child of each entry of "kandidatenliste". Right now, it is being instantiated right underneath it. alt text

I have found a couple questions which sounded the same, but I have not found a question where the to-be-instantiated object needs to be the child of a to-be-instantiated list item...

Help would be appreciated :)

unbenannt.png (5.7 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

1 Reply

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

Answer by BastianUrbach · Apr 14, 2018 at 08:57 PM

Shortest option (requires System.Linq):

heiratskandidaten.Add (Instantiate(heiratskandidatPrefab, kandidatenliste.Last() ));
The same without Linq:
heiratskandidaten.Add (Instantiate(heiratskandidatPrefab, kandidatenliste[kandidatenliste.Count - 1] ));
And a version that is more readable imo but a bit longer:
var kandidat = Instantiate(npcs.npc[i], KupplerinTransform);
var heiratskandidat = Instantiate(heiratskandidatPrefab, kandidat );
kandidatenliste.Add (kandidat);
heiratskandidaten.Add (heiratskandidat);
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 Mahunreah · Apr 14, 2018 at 10:01 PM 0
Share

Thank you so much :D! I've tried your solutions, but none seemed to work - but they brought me to the right solution! I hadn't understood that a list entry could be its own Transform in my hirarchy =D Based on that, I added a temporary transform for the last entry in the list and now, it works like a charm :)!

 kandidatenliste.Add(Instantiate(npcs.npc[i], $$anonymous$$upplerinTransform));
             Transform tmpTransform = kandidatenliste[i].transform;
             heiratskandidaten.Add (Instantiate (heiratskandidatPrefab, tmpTransform));

Now, I wonder why "kandidatenliste.Last()" and "kandidatenliste[kandidatenliste.Count - 1]" didn't work... But I will leave that particular head-scratcher to tomorrow.

Thanks a ton, you've helped me a lot =)!

avatar image BastianUrbach Mahunreah · Apr 14, 2018 at 10:05 PM 0
Share

Hm, do your lists contain GameObjects or Transforms? Only a transform can be set as parent so that might be the reason.

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

96 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

Related Questions

Instantiated Bullets Not Picking Up Correct Position/Rotation 2 Answers

[SOLVED] How can I check what was the last instantiated object? 2 Answers

How would I make this Cloned Function Copy to an endless amount? 1 Answer

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

Is Instantiating bullets/many objects always bad for performance? 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