Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by N0bo · Feb 13, 2015 at 09:59 AM · c#parenting

How to parent GameObjects to a newly instantiated empty, if at all possible

Hi, fellow Unity 3d users.

I'm pretty new to Unity 3d and scripting in general.

I'm working on a falling blocks game. In the game there are multiple colored blocks of which each color of block will have it's own Tag. The blocks have trigger colliders that fire when any block touches an adjacent block

I'm trying to write a script on OnTriggerEnter that will spawn an empty prefab and set set that new empty as a parent for all the adjacent blocks. My reasoning behind this is that the empty will give me a nice centalized location for adding a script to check if any of the blocks are currenty resting on any other blocks since same colored blocks become attached to one another.

The script i've written doesn't give any errors in the compiler or Unity but the blocks doesn't get parented to the newly Instatiated empty (prefab). I'd apreciate any insights into this.

tl;dr Is it possible to create an empty during the game and then add gameobjects already in the game to it ? and how to do this ?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class BlockClusters : MonoBehaviour 
 {
     public GameObject cluster;
     public List<GameObject> colliders = new List<GameObject>();
   
     public int getSameBlox;
 
 
     void OnTriggerEnter (Collider other)
     {
 
 
         if (other.tag == gameObject.tag)
         {
             
             
             if(colliders.Contains (other.gameObject) != true)
             {
                 colliders.Add (other.gameObject);
                 getSameBlox = getSameBlox + 1;
                 Debug.Log (other + " was added to the array");
                 Debug.Log ("the length of the array is " + getSameBlox);
             }
             Debug.Log (gameObject.transform.parent);
             
             
         }
 
         Transform clusterPos = gameObject.transform;
 
         for (int i = 0; i < colliders.Count ; i++) 
         {
             if (colliders[i].transform.parent == null && gameObject.transform.parent == null)
             {
                 Transform newEmpty =  GameObject.Instantiate (cluster, clusterPos.position, clusterPos.rotation) as Transform;
                 colliders[i].transform.parent = newEmpty;  
                 gameObject.transform.parent = newEmpty;
                 Debug.Log (gameObject.transform.parent);
             }
             
         }
     }
 
 }
 











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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by awest · Feb 14, 2015 at 06:16 PM

To create an empty GameObject you can use

 GameObject parentObject = new GameObject(); //create an 'empty' object
 object.name = "BlockRow";             //set it's name

To Parent objects just use their transform like so.

 Transform child;
 child.parent = parentObject.transform;
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 aditya · Jul 02, 2016 at 01:13 PM

Do not instantiate as Transform keep instantiating as GameObject and then get its transform component while parenting ... Like below ...

              if (colliders[i].transform.parent == null && gameObject.transform.parent == null)
              {
                  GameObject newEmpty =  GameObject.Instantiate (cluster, clusterPos.position, clusterPos.rotation);
                  colliders[i].transform.SetParent (newEmpty.transform, true);  
                  gameObject.transform.SetParent (newEmpty.transform, true);
                  Debug.Log (gameObject.transform.parent);
              }
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 EDevJogos · Jul 02, 2016 at 01:42 PM

I had some problemens with transform.parent = "newParent", try using the SetParent() Method:

https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

I don't really understand your code though, it seams to me you want something like:

//Save a pointer to all adjacent blocks.

//Set them as child to the same parent.

And i don't belive the result of the current code will be this.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Dynamic parenting to moving platform so object won't slide from it on it own doesn't work properly 1 Answer

Is it possible to reparent an object to the root of a scene? 2 Answers

Moving gameobject to Sibling's child ("Niece?").,Moving Gameobject into sibling's child ("Cousin?") c# 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