Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 capucinebuddy · Jan 10, 2020 at 10:35 AM · instantiatemovechildren

Children Not Moving With Parent From Instantiation?

Hi there,

In my project I have a central empty object called Game which contains several game objects that interact together as children. I am trying to position one particular child in the top left of the screen along with all of its children after instantiating it.

To do this I am doing the following in Game

 GameObject child = Instantiate(childReference, new Vector3(0, 0, 0), Quaternion.identity);
 child.transform.parent = transform;
 Vector3 cameraPoint = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 10));
 child.transform.position = cameraPoint;

This successfully moves the child where I am expecting it to but all the children of GameObject created are offset the amount moved so that they remain at the original 0,0,0 position.

Any Ideas?

Thanks

Comment
Add comment · Show 9
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 bhavinbhai2707 · Jan 10, 2020 at 11:04 AM 0
Share

can you please be more clear about your question? ps: make sure you don't have a rigid-body attached to your gameobject or else it will be under control of physics and won't move with parent.

avatar image capucinebuddy bhavinbhai2707 · Jan 10, 2020 at 11:55 AM 0
Share

No rigid bodies attached. Let me clarify. I have one central game object that this script is attached to. I then instantiate, from a reference to a prefab, an object which contains several children. I call this new object "child". Let "child" have 5 children for example. I then move child to the corner of the screen. The object child moves to position say -5,-5 which is indeed the bottom left. The issue is all five children are set to position 5,5 so they in turn are not moving.

avatar image Develax · Jan 10, 2020 at 11:34 AM 0
Share

You're moving only one child that is just created, why do you expect other children to be moved?

avatar image capucinebuddy Develax · Jan 10, 2020 at 11:56 AM 0
Share

As the other children are children of the object being moved. I wasn't clear enough but there is the object which this script, the child object which is being moved, and children of child which are supposed to be moved with it but are not.

avatar image Develax capucinebuddy · Jan 10, 2020 at 12:21 PM 0
Share

I checked it and it works just fine. $$anonymous$$ake sure that you have children of the child exactly in its prefab.

avatar image capucinebuddy · Jan 10, 2020 at 12:13 PM 0
Share

Sort of something weird I found. This problem occurs only if I instantiate the child and its respective children at run time.

If I add the object child and its children to the hierarchy in the inspector and run the same program with a reference to the child rather than the Instantiated object the child and respective children all move uniformly as expected.

avatar image Develax capucinebuddy · Jan 10, 2020 at 12:24 PM 0
Share

It would be hard to infer from the code of your question. There is nothing like instantiating children and nobody knows which way you did it.

avatar image capucinebuddy Develax · Jan 10, 2020 at 01:44 PM 0
Share

Let me post some photos of what I am doing since my process is not clear

avatar image capucinebuddy · Jan 10, 2020 at 01:52 PM 0
Share

Here are some photos of my process and the direct code.

alt text

In this photo above I placed FullDeck as a child of Game before running the program, when the following code ran the results were as expected where just the coordinates of FullDeck were changed and none of its children cards moved

     public GameObject fullDeckRef;
     public GameObject emptyDeckRef;
     public GameObject fullDeck;
     // Start is called before the first frame update
     void Start()
     {
         float xOffset = 1f;
         float yOffset = 3.111111f / 2f+1;
         Vector3 cameraPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 10));
         Debug.Log(cameraPoint);
         cameraPoint.x -= xOffset;
         cameraPoint.y -= yOffset;
         fullDeck.transform.position = cameraPoint;
     }

I then got rid of FullDeck and changed the code to have the following two lines which instantiates the FullDeck and its children from the reference.

     public GameObject fullDeckRef;
     public GameObject emptyDeckRef;
     public GameObject fullDeck;
     // Start is called before the first frame update
     void Start()
     {
         float xOffset = 1f;
         float yOffset = 3.111111f / 2f+1;
         fullDeck = Instantiate(fullDeckRef, new Vector3(0, 0, 0), Quaternion.identity);
         fullDeck.transform.parent = transform;
         Vector3 cameraPoint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 10));
         Debug.Log(cameraPoint);
         cameraPoint.x -= xOffset;
         cameraPoint.y -= yOffset;
         fullDeck.transform.position = cameraPoint;
     }

This then produced the following result alt text

In this instance FullDeck is at the same position, 8.0, 3.4, but in this second case all of the FullDeck's children have been set to position -8.0,-3.4

screen-shot-2020-01-10-at-54235-pm.png (333.0 kB)
screen-shot-2020-01-10-at-54308-pm.png (442.2 kB)

1 Reply

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

Answer by capucinebuddy · Jan 11, 2020 at 09:18 AM

Solution found!

Turns out you have to use a transform.localPosition otherwise the position is placed in global coordinates

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 iGoA · Aug 08, 2021 at 03:15 PM 0
Share

Thanks for posting the solution. Just struggled with the same problem here. :)

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

146 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 avatar image avatar image avatar image avatar image 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

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers

move childA to position of childB 1 Answer

Instantiate VS Transform 1 Answer

Make multiple Projectiles move at the same Time... 0 Answers

Create Clones as children in loops? 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