- Home /
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);
}
}
}
}
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;
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);
}
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.