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 Ziya · Apr 17, 2017 at 04:11 PM · instantiatetransformparentparenting

How to instantiate a GameObject in a scene and keep world transform?

There is next in API:

 public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);

But I need next:

 public static Object Instantiate(Object original, bool instantiateInWorldSpace);

I need to instantiate it in a scene without parenting, but keep world transform.

Comment
Add comment · Show 2
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 ExtinctSpecie · Apr 17, 2017 at 04:56 PM 0
Share

i dont know but did you try setting the second parameter ( i dont know if it works just asking ) ?

avatar image Ziya ExtinctSpecie · Apr 17, 2017 at 08:04 PM 0
Share

I tried. No, it doesn't work.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Joe-Censored · Apr 17, 2017 at 05:00 PM

Just instantiate it without assigning a parent. You only need to set instantiateInWorldSpace when you're assigning a parent during instantiation. Otherwise without a parent all you have is world space, so that bool would be meaningless even if it were available.

public static Object Instantiate(Object original);

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

Comment
Add comment · Show 3 · 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 Ziya · Apr 17, 2017 at 08:03 PM 0
Share

It works only if I instantiate an object that is already in world space, but it doesn't work if the object is parented.

avatar image Joe-Censored Ziya · Apr 17, 2017 at 09:38 PM 0
Share

I don't understand. If you're not parenting it to anything there is no local space, because local space is local to the parent. No parent means you only have world space. What exactly is the problem you're having?

avatar image Ziya Joe-Censored · Apr 18, 2017 at 08:47 AM 0
Share

It is simple, but we have misunderstanding.

Reproduce:

  1. Create Cube.

  2. Place Cube in parent.

  3. Reset transform of Cube, local position set to zero.

  4. $$anonymous$$ove parent to another location.

  5. Instantiate Cube in script.

It appears at the center of world space, but I want it to not change its position, just stay same. I can reach my goal by creating default root parent and using first constructor in my question by setting instantiateInWorldSpace to true.

I made a video: https://www.youtube.com/watch?v=bgi-cemXWGE&

avatar image
2

Answer by LilBoWeep · Apr 18, 2017 at 09:15 AM

Why not just do it this way?

 GameObject nameOfGameObeject = Instantiate (Resources.Load ("preFab"), transform.position, new Quaternion ()) as GameObject;
 nameOfGameObject.position = newPosition; 

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 Ziya · Apr 18, 2017 at 09:25 AM 0
Share

This is what I used doing, but I am interested, if there is a built-in way, turns out no, there is no such a way. I guess it is because there is no root transform. I am talking about transform.root described in API, but about the transform one level higher.

avatar image
0

Answer by Dray · Nov 27, 2017 at 03:34 PM

The reason this is happening to you is that the transform.position value is always relative to the parent (or much more the whole transform is).


The first cube is assigned to your parent GameObject that's positioned at (-5, 0, 0) in the video. For that reason, the child cube with the transforms local position being set to (0, 0, 0) is shown at (-5, 0, 0) in the world space (~both positions added).


If you copy that cube and paste it somewhere else like that, it's position will be relative to its new parent, which is in this case the - let's call it - 'root' transform, that (if it existed) had a position (0, 0, 0), rotation (0, 0, 0) and a scale (1, 1, 1).


Maybe you can try to instantiate the second cube keeping the parent as @LilBoWeep and @Joe-Censored suggested and then afterwards move it outside. I know that this works when you are doing it by hand in the hierarchy but never tested it from code, would be interestening to know!

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

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

90 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

Related Questions

Getting transform from an instantiated clone to parent it? 1 Answer

Insantiate object to parent without changing scale? 0 Answers

how do i know where my instantiated object came from? 2 Answers

Parent mesh problem 0 Answers

Parenting Instantiated objects on Collision 2 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