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 RaduMitroi · Jan 11, 2019 at 02:39 AM · gameobjectinstantiatepositionparentparent transform

Keep instantiated GameObject's position while parenting it to another GameObject

I want to instantiate a GameObject to a position and then change its parent without changing any of its position. I have two GameObjects, the active points in the following images: alt text

alt text

I am instanting a prefab at the active point from the first image (the big cube):

 GameObject theTile = Instantiate(thePrefab, spawningZone.transform);

Where spawningZone is the big cube. I then want to parent the instantiated object to the active point from the second image (that is the GameObject the script is applied on):

 theTile.transform.parent = transform;

But doing this moves my instantiated object to the active point in the second image, instead of staying where I've instantiated it.
I've tried using

 theTile.transform.SetParent(transform, false);

But I get the same result. I've also tried saving the local position of the instantiated object before doing the parenting, doing the parenting and then setting back the local position to the one I've saved, but it still doesn't work.

Comment
Add comment · Show 3
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 RaduMitroi · Jan 11, 2019 at 05:16 PM 0
Share

Any ideas from anyone?

avatar image TreyH · Jan 11, 2019 at 05:42 PM 0
Share

There's an optional second parameter (worldPositionStays) for SetParent, which you are strangely setting to false in your example.

 theTile.transform.SetParent(transform, true);


avatar image RaduMitroi TreyH · Jan 11, 2019 at 06:41 PM 0
Share

I tought setting that parameter to false would do it, but I've tried with it set to false and true, but same thing happened.

3 Replies

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

Answer by RaduMitroi · Jan 16, 2019 at 12:22 PM

I found out what my problem was. It was another line of code that I was using to spawn these prefabs in a grid-like manner and I was messing with the instantiated object's local position after I was setting the new parent. Putting the line of code for setting the parent after messing with the local position solved the issue.

Comment
Add comment · 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
1

Answer by Kudorado · Jan 11, 2019 at 02:44 AM

Try setting position instead of localPosition. Here is an example:

 var pos =  thePrefab.transform.position;
 GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
 theTile.position = pos;

hope it help.

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 RaduMitroi · Jan 11, 2019 at 03:02 AM 0
Share

Thank you, but that doesn't do it. The object I'm creating still moves itself to the GameObject I'm parenting it to.

avatar image
1

Answer by Robotic_Soul · Jan 15, 2019 at 04:33 AM

You know you can pass the parent in the Instantiate call itself? Check out the scripting manual page [1] on it. I would also reccomend setting the vector3 manually... possibly from some variable which you can access and play with at runtime. Something simple like this:

 public float SomePosX=0;
 GameObject new_cola = Instantiate(prefabObj,  new Vector3(SomePosX, SomePosY, 0), newParent.rotation, newParent);

Also you should be aware of the object's center, because that's where it will spawn if you target an object. It's easy to get confused in unity because there is an option next to the default move/scale tools called pivot/center which can make it seem like the object is offset (possibly because of some other large or offset attached object) it's also next to, and relevant to Global/Local button. Be aware of the objects true center when referencing it's transform.

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

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 ChristmasEve · Jan 01, 2021 at 06:35 PM 0
Share

This didn't work. I tried setting the parent in the Instantiate statement so that I'm setting the world position at the same time, but I get the same result. $$anonymous$$y object does not appear at the position (like new Vector3(SomePosX, SomePosY, 0), from your example but with my position).

This is driving me crazy. If I instantiate an object where I want it and don't parent it and run it like that, it appears in the right place, so I hit pause and drag the object manually to the object I want it parented to, and it works! It doesn't move. The object stays at the same world position. I tried a dozen different ways, including yours, to do it programmatically, and it appears in some strange location (nearby) under the ground. Something this simple shouldn't be so hard. I've created outline shaders, I've written an $$anonymous$$$$anonymous$$ORPG server but I can't get an object to appear at a particular world position but while parented to another.

EDIT: let me add a little more info here. The GameObject I'm parenting this to is a child of some huge tree of twisted gameobjects with all different scales and orientations. But that shouldn't matter if I'm specifying the world position, right? How do you escape from all that complexity and say "I just want to place you HERE" but still keep it parented so that when the main object is deleted, so will this GameObject I'm adding.

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

175 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 GameObject gets spawned as a child 2 Answers

Instantiate a GameObject at the position of one of its child objects 1 Answer

Find position of the GameObject this script is attached to. 2 Answers

GameObject Instantiated in Wrong Position 1 Answer

Problem with Parent and Child using LookAt 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