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.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BlockClusters : MonoBehaviour
{
public GameObject cluster;
public List colliders = new List();
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);
}
}
}
}
↧