Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Muzz 1 · Jan 25, 2011 at 06:46 PM · gameobjectinstantiatechild

Instantiate as child

I'm trying to make it so that when a collision happens, an object is instantiated at an exact point as the child of another object. How can I do this?

Comment
Add comment · Show 1
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 Bunny83 · Jan 25, 2011 at 07:04 PM 0
Share

Please add next time more details because your question is very abstract: when "a collision" happens; child "of another" object. And since you didn't post any code you have so far, we only can guess what language you use / want use.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jan 25, 2011 at 07:16 PM

I guess you use JS (because most beginners use JS ;) but i prefer C#)

One way would be something like:

var yourPrefab : Transform; var anotherObject : Transform;

function OnCollisionEnter (collision : Collision){ var anObject : Transform; anObject = Instantiate(yourPrefab, Vector3.zero, Quaternion.identity); anObject.parent = anotherObject; anObject.localRotation = Quaternion.identity; // rotates the object like the parent anObject.localPosition = Vector3(XX,YY,ZZ); // define here your "exact point" within the parent }

You have to give more information whether you want a exact local or global position and if one of your "objects" is involved in the collision?

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
0

Answer by Jessy · Jan 25, 2011 at 07:04 PM

You instantiate first, then you parent. You can always set position, and after the parenting, set localPosition if you want, instead.

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
0

Answer by Ziron999 · Mar 08, 2014 at 05:35 AM

in c# it had to be done like this. Adding this info for anyone else searching for this solution.

 GameObject player;
 GameObject playerClone;
     void Start () {
         player = Resources.Load("Player") as GameObject;
         Instantiate(player);
         playerClone = GameObject.Find("Player(Clone)");
         playerClone.transform.parent = GameObject.Find("Main Camera").transform;
     }
Comment
Add comment · Show 2 · 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 fifthknotch · Mar 08, 2014 at 05:39 AM 0
Share

$$anonymous$$inda an old question but thanks

avatar image Mark Baker · Mar 19, 2014 at 11:46 AM 0
Share

You should avoid using GameObject.Find() where possible as it will be relatively slow. In this case you can simply cast the newly created player object:

  GameObject player;
  GameObject playerClone;
 
  void Start () {
      player = Resources.Load("Player") as GameObject;
      playerClone =  Instantiate(player) as GameObject;
      playerClone.transform.parent = GameObject.Find("$$anonymous$$ain Camera").transform;
  }

Note that if you only want to parent to the current game object (which the script is attached to) you can just use playerClone.transform.parent = transform. Which will save you another GameObject.Find()

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to copy a Script as a child of another object 1 Answer

Clone Selector 1 Answer

Renaming Child Objects during Instantiate? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

It is not possible to invoke an expression of type 'UnityEngine.GameObject'? 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