public void Build()
 {
     foreach (Transform child in transform)
     {
         DestroyImmediate(child.gameObject);
     }
     br = buildingRules.Build(gameObject, new Vector3(0, 0, 0));
 }
        public BuildingResult Build(GameObject parent, Vector3 initialPosition)
        {
            Check();
            GameObject subRulesContainerObject = new GameObject(this.name);

            subRulesContainerObject.transform.parent        = parent.transform;
            subRulesContainerObject.transform.localPosition = initialPosition;
            Vector3        position = new Vector3(0, 0, 0);
            Bounds         bounds   = new Bounds(position, new Vector3(0, 0, 0));
            BuildingResult br       = new BuildingResult();

            for (var i = 0; i < size; ++i)
            {
                if (subRules != null && subRules.Count > 0)
                {
                    foreach (BuildingRules subRule in subRules)
                    {
                        BuildingResult nextResult = subRule.Build(subRulesContainerObject, position);
                        br.childResults.Add(nextResult);
                        bounds = CombineBounds(
                            new Bounds(bounds.center, bounds.size),
                            new Bounds(nextResult.bounds.center + position, nextResult.bounds.size));
                        position = Vector3.Scale(bounds.max - bounds.min, BuildDirectionFilter());
                    }
                }

                if (mesh != null)
                {
                    GameObject meshRenderer = new GameObject(
                        mesh.name + " Mesh"
                        );
                    meshRenderer.AddComponent <MeshFilter>().mesh = mesh;
                    meshRenderer.AddComponent <MeshRenderer>();
                    meshRenderer.AddComponent <BoxCollider>();
                    meshRenderer.transform.parent        = subRulesContainerObject.transform;
                    meshRenderer.transform.localPosition = new Vector3(0, 0, 0);
                    bounds = CombineBounds(bounds, mesh.bounds);
                }

                if (prefab != null)
                {
                    GameObject meshRenderer = Instantiate(prefab);
                    Renderer   r            = meshRenderer.GetComponent <Renderer>();
                    meshRenderer.transform.parent        = subRulesContainerObject.transform;
                    meshRenderer.transform.localPosition = new Vector3(0, 0, 0);
                    if (r != null)
                    {
                        bounds = CombineBounds(bounds, r.bounds);
                    }
                }
            }

            return(new BuildingResult {
                bounds = bounds,
                entity = subRulesContainerObject
            });
        }