• 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
Question by wiggs1111 · Jun 08, 2015 at 10:58 AM · gameobjectinstantiateparentchild

Instantiate object as child of exsisitng game object

Im trying to test the created level in another scene but need to use DontDestoryOnLoad in awake function. How do I instantiate my new objects as a child of an exsisting game Object?

//#pragma strict import System.Collections.Generic;

 var BlockPositions : List.<Vector3> = new List.<Vector3>();
 var collide : boolean = false;
 var endCube = gameObject;
 var prefab = gameObject;
 var testPlayer : boolean = false;
 
 function Awake(){
     //DontDestoryOnLoad();
 }
 
 function OnCollisionEnter(coll: Collision){   //Colliding with object?
         if(coll.gameObject.tag=="floor"){
             collide=true;    
             }
 }
 function OnCollisionExit(coll: Collision){
             collide=false;            
 }
 
 
 function Update () {
 myPosition = transform.position;
 
     if(Input.GetButtonDown("up")){
         transform.position.y = transform.position.y+1;
         if (collide == false){
             collide = true;
         }
     }
 
     if(Input.GetButtonDown("down")){
         transform.position.y = transform.position.y-1;        
         if (collide == false ){
             collide = true;
         }
     }
 
     if(Input.GetButtonDown("left")){
         transform.position.x = transform.position.x-1; 
            if (collide == false){
                  collide = true;
         }
     }
 
     if(Input.GetButtonDown("right")){
         transform.position.x = transform.position.x+1;
            if (collide == false){
                collide = true;
         }
     }
         
     if (Input.GetButtonDown ("Submit")){      
         if (BlockPositions.Contains(transform.position))
         {
        //     DestroyImmediate(prefab);
             testPlayer = true;
             Instantiate(endCube, transform.position, Quaternion.identity);
             
             BlockPositions.Add(transform.position);
 
             testLevel();
             /*
                 NOW GIVE USER GUI OPTION TO RETRY LEVEL/SUBMIT LEVEL
             */
         }
     }
     Debug.Log(myPosition);    
     
     if (collide == false){
         collide = true;
         Instantiate(prefab, transform.position, Quaternion.identity);
 
         BlockPositions.Add(transform.position);
         
     }
 }
 
 function testLevel(){
 WaitForSeconds(2);
         Application.LoadLevel("TestLevel");
 }


Comment
Chrism24747

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Cherno · Jun 08, 2015 at 11:08 AM

You can't do it. However, what you can do is instantiate it normally, and immediately afterwards set it's parent to whatevery GameObject's transform you want.

 gameObject parentObject;
 
              Instantiate(endCube, transform.position, Quaternion.identity);
 endCube.transform.parent = parentObject.transform;
Comment
HarshadK
wiggs1111
zab
unity_6IF83HwnSYtuGw
ianny_dev
Vinnihu
Handy333

People who like this

7 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 wiggs1111 · Jun 10, 2015 at 08:56 AM 0
Share

Thank you for your help!

avatar image

Answer by JparrishAU · May 14, 2019 at 05:30 PM

Just FYI. You can do this directly now using the overloaded Instantiate method.

 Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Comment
lastgrunger
Semihsmg
mateov951
ianny_dev
Filip8429
Abishekh_Parivel
MakeStuffJohn

People who like this

7 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 riis5 · May 19, 2019 at 09:31 AM 0
Share

Wow! Thanks a lot!

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate GameObject Parent targetting issue. 0 Answers

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

Renaming Child Objects during Instantiate? 1 Answer

Make a simple tree 1 Answer

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