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 sos_willborn · Jul 09, 2017 at 03:33 PM · unity 5parent-child

Instantiate as a Child of the Parent in for loop

Hello, everyone I'm trying this code

 public GameObject group;
 public GameObject spriteofobject;

 void Start(){
 int sekilKareSayi;
 
 var rotation = transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);
 for (sekilKareSayi = 1; sekilKareSayi == 1; sekilKareSayi++)
             {
                 var position = transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0.0f);
                 var create = Instantiate(spriteofobject, position, rotation);
 
                 create.transform.parent = group.transform;
                 for (sekilKareSayi = 1; sekilKareSayi <= 3; sekilKareSayi++)
                 {
                     var position1 = transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y - 0.320f, 0.0f);
                     var create1 =  Instantiate(spriteofobject, position1, rotation);
 
                     create1.transform.parent = group.transform;
                 }
 
               
             }
 }

but it's not creating 'create' and 'create1' as a child of parent 'group' object

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 FortisVenaliter · Jul 09, 2017 at 03:37 PM 0
Share

Hmmm... that looks like it should work... Do those objects use RectTransforms ins$$anonymous$$d of Transforms?

avatar image sos_willborn FortisVenaliter · Jul 09, 2017 at 03:43 PM 0
Share

no, they aren't

avatar image FortisVenaliter sos_willborn · Jul 09, 2017 at 03:44 PM 0
Share

Is the group variable null in the inspector?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cstooch · Jul 09, 2017 at 05:12 PM

Is this for troubleshooting??

  for (sekilKareSayi = 1; sekilKareSayi == 1; sekilKareSayi++)

Why are you using a For loop to do something that can only iterate once? On top of that, you're using the same variable name in your nested loop, which can cause unexpected results if you haven't really thought out what will happen with this variable. Anyways, that's not the issue here, I wouldn't think, so...

Your reference "group"... did you set this to an instance of group? Or a prefab? Ensure it's an instance. I think you'd get an error if it was a prefab though. Also, another way of doing this... you can actually instantiate with a parent specified:

  var create = Instantiate(spriteofobject, group.transform);

Maybe that will give you better results, although I can't see why the way you did it would not work. When you look in the hierarchy, where does your object show parented to?

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 sos_willborn · Jul 09, 2017 at 06:17 PM 0
Share

I solved the problem. It was creating random thing. I forgot to add code block sorry.

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

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

Can't Detach children on collision of BoxCollider. 1 Answer

Unity 3D : Objects that were parented under empty object are changing their position after I hit play 0 Answers

I get this error when i create a project. I tried reinstalling unity but this didn't fix anything. Anyone know how to resolve this error? 1 Answer

Weird FPS and CPU usage with Unity5 0 Answers

unity unable to convert to dex format, need help fast. 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