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 DubSket · May 04, 2014 at 03:33 PM · parent transform

Children not working with Parent Object properly

Hi guys,

I've come across an error that's got me stumped.

I'm working on some code that will instantiate blocks (using a prefab) into a certain arrangement; a bigger cube. As the cubes are spawned they should become the children of an existing game object created solely for holding this blocks. Then in the FixedUpdate() I perform some basic physics; making the giant cube of individual blocks turn as if they were all one object.

The problem I'm getting, however, is that the blocks are spawned into their correct arrangement, then themselves do nothing. I noticed that while they are not moving, a new block seems to have been created inside of them and is actually working the way I would expect. This is not what I want, though. As mentioned I'd prefer the original blocks move as one.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class CubeSpawn : MonoBehaviour {
 
     private Transform[] allChildren;
     private GameObject parentCube;
     public GameObject Cube;
     private Vector3 spawnPos = new Vector3(-4.83f,5f,5f);
     private int cLineMax = 2; //max cube spawn int
     private float distanceBetweenCubes = 1.5f;
 
     // Use this for initialization
     void Start () {
 
         //parentCube.AddComponent<Rigidbody>();
 
         for(int i=0; i<cLineMax; i++)
         {
             for(int j=0; j<cLineMax; j++)
             {
                 for(int k=0; k<cLineMax; k++)
                 {
 
                     parentCube = Instantiate(Cube, 
                                               new Vector3 (spawnPos.x + i*distanceBetweenCubes, 
                                             spawnPos.y + j*distanceBetweenCubes, 
                                              spawnPos.z + k*distanceBetweenCubes),
                                             Quaternion.identity) as GameObject;
                     //Instantiates the cubes into a parent gameobject
 
                 }
             }
         }
         allChildren = parentCube.GetComponentsInChildren<Transform>();
         //grabbing every cube in the parent  transform
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void FixedUpdate () {
 
         foreach (Transform child in allChildren) {
 
             child.transform.Rotate(Vector3.right * Time.deltaTime);
 
         }
     }
 }    
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

If anyone is able to point out where I'm going wrong (likely it's multiple places) I'd REALLY appreciate it; this has me stumped.

Thanks guys.

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 DMGregory · May 04, 2014 at 03:56 PM

GameObjects created with Instantiate() sit at the top level of the hierarchy by default.

If you want them parented to another object, do something like this...

 GameObject child = Instantiate(prefab, worldspacePosition, worldspaceRotation) as GameObject;
 child.transform.parent = parent.transform;

Also, note that in your cube-creation loop you are continuously redefining parentCube. At the end of that loop, the variable parentCube is pointing only at the last-created cube. So GetComponentsInChildren() will only return the transforms inside that cube, not the other cubes you've spawned.

Finally, even if you had the parenting set up the way you intend, child.transform.Rotate() would not make the whole collection of children "turn as if they were all one object." To do that, you would want to rotate their parent. Rotations applied to the children will occur along their own local axes, not the parent's axes.

Comment
Add comment · Show 5 · 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 DubSket · May 04, 2014 at 04:44 PM 0
Share

Hi, thanks for your reply - it was really helpful. I do still have a few questions, though. If it wouldn't be too much trouble just clearing some things up.

Firstly, the child.transform.parent = parent.transform; - I'm guessing I just leave this in the loop underneath the instantiate instuction? Also will this provide a fix to the constant redefining of parentCube? If not, then my third question would be how can I go about doing this?

Once again, thanks for your time.

avatar image DMGregory · May 04, 2014 at 05:20 PM 1
Share

Your loop should look something like...

 parentCube = new GameObject("parent");
 parentCube.transform.position = Vector3.zero; // Or wherever you want the center of the cube to be.
 
 for(i, j, k...) // Abbreviating to fit in a comment.
 {
    GameObject child = Instantiate(Cube) as GameObject; // Add initial position
    child.transform.parent = parentCube.transform;
 }

Note that parentCube is assigned exactly once, at the very top. I use a temporary variable to hold each child reference only as long as I need it.

avatar image DubSket · May 04, 2014 at 05:57 PM 0
Share

Hi,

I tried the first line in the constructor but I got an error telling me "Unexpected symbol = in class, struct, or interface member declaration", and also the same message about the "parent" part of the line. So, I tried it in the start function and ins$$anonymous$$d got errors telling me "The name parentCube does not exist in the current context". Do you have any idea why this might be?

     public GameObject Cube;
     private Vector3 spawnPos = new Vector3(-4.83f,5f,5f);
     private Vector3 parentCubeCentre = new Vector3(-2.415f,5f,5f);
     private int cLine$$anonymous$$ax = 2; //max cube spawn int
     private float distanceBetweenCubes = 1.5f;
 
     // Use this for initialization
     void Start () {
         parentCube = new GameObject("parent");
         parentCube.transform.position = parentCubeCentre;
 
         for(int i=0; i<cLine$$anonymous$$ax; i++)
         {
             for(int j=0; j<cLine$$anonymous$$ax; j++)
             {
                 for(int k=0; k<cLine$$anonymous$$ax; k++)
                 {
                     GameObject child = Instantiate (Cube,
                     //parentCube = Instantiate(Cube, 
                                               new Vector3 (spawnPos.x + i*distanceBetweenCubes, 
                                             spawnPos.y + j*distanceBetweenCubes, 
                                              spawnPos.z + k*distanceBetweenCubes),
                                             Quaternion.identity) as GameObject;
                     //Instantiates the cubes into a parent gameobject
 
                     child.transform.parent = parentCube.transform;
                 }
             }
         }

     }

Thanks for being so helpful :)

avatar image DMGregory · May 04, 2014 at 06:05 PM 1
Share

You used to have:

 private GameObject parentCube;

in the class body (ie. between "public class CubeSpawn" and "Start") - why did you get rid of it? You can't use a variable that you haven't declared anywhere.

avatar image DubSket · May 04, 2014 at 06:15 PM 0
Share

Oops!! $$anonymous$$y bad, I thought the " parentCube = new GameObject("parent");" was a new declaration and therefore the original wasn't needed. I just tried this now and it appears to work!

Thanks so much, you have no idea how helpful this is.

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

21 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

Related Questions

fixing rotation on a specific axis 1 Answer

Impossible to rotate child using parent rotation gizmo 1 Answer

Parenting instantiated particle system 1 Answer

Particle orientation from parent 0 Answers

Detach a child from a parent 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