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 Jimmiefan · Jul 02, 2018 at 03:05 PM · gameobjectinstantiatetransformparentchild

Creating new Transform from existing objects Transform to Instantiate object

I've got an Empty game object that will be an object spawner, but I need the objects that spawn to vary slightly along the x-axis some, to then instantiate. Using the code below, I've got it's random position moving just fine using Random.Range, but it's shifting the Empty game object with this script on it each time it instantiates an object, rather than instantiating the object at the different transform after changing it's position in relations to the spawner.

 private Transform newSpawnTransform;

 void Start ()
 {
     newSpawnTransform = transform;
 }

 void randomForce()
 {
     newSpawnTransform.position += new Vector3 ((Random.Range (-.1f, .1f)), 0f, 0f);
 }

Then I'm Calling the randomForce function, and instantiating the object when needed.

     randomForce ();
     Instantiate (object, newSpawnTransform);

Perhaps I just need clarification on how the transforms work, since it moves the parent object to instantiate the objects, rather than use the parent transform, then instantiate it nearby. I also looked at this question on how to instantiate objects without becoming a child in hopes of changing where it spawns, but couldn't get it to work. https://answers.unity.com/questions/1401222/instantiate-an-object-without-being-the-child-of-a.html

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
0
Best Answer

Answer by Shameness · Jul 02, 2018 at 04:55 PM

See the Instantiate reference, there is more than one constructor: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html .

One you are using is:

public static Object Instantiate(Object original, Transform parent); .

I suggest you to use one with in the first example in the previous doc link.

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

Answer your case:

Instantiate (object, newTransform.position, Quaternion.identity);

Comment
Add comment · Show 7 · 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 Jimmiefan · Jul 02, 2018 at 05:41 PM 0
Share

Thank you for the quick help. I understand that adding .position and Quaternion.identity makes the instantiated objects not children, but it doesn't solve my main problem of why this newTransform is moving the whole gameobject to spawn other objects, rather than using that position to spawn objects separate from where the spawner is.

avatar image Shameness Jimmiefan · Jul 02, 2018 at 06:01 PM 0
Share

Changing position of the parent object affects children as well. Your newSpawnTransform holds same reference (or pointer) with original transform of the empty game object.

avatar image Jimmiefan Shameness · Jul 02, 2018 at 06:09 PM 0
Share

I see, so what would I need to do to change the reference? Would I be able to set each of the newTransform positions separate, rather than setting it by setting it equal to transform? The objects instantiated are no longer spawning as children, so I'm not sure what you mean with "Changing position of the parent object affects children as well"

Show more comments

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

155 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

Related Questions

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

Instantiating a new gameObject as a child of a different gameObject 2 Answers

Instantiate a Prefab as child 0 Answers

Instantiated GameObject gets spawned as a child 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