// This function destroys the object on the given position. private void RemoveObject() { int buildingLayerMask = 1 << LayerMask.NameToLayer("Building"); Vector3 destroyPoint = new Vector3(_xPos, 0, _zPos); var hitCollider = Physics.OverlapBox(destroyPoint, Vector3.one / 4, Quaternion.identity, buildingLayerMask); if (hitCollider.Length > 0) { for (int i = 0; i < hitCollider.Length; i++) { Building building = hitCollider[i].gameObject.GetComponent <Building>(); if (building) { BlockProgrammingWindow.DestroyBuilding(building.buildingType); } Object.Destroy(hitCollider[i].gameObject); } } else { // Show to player that is not possible because space is not occupied return; } }
// This function spawns the object on the given position. private void PlaceBuilding() { if (!_building) { return; } int buildingLayerMask = 1 << LayerMask.NameToLayer("Building"); Vector3 spawnPoint = new Vector3(_xPos, 0, _zPos); var hitCollider = Physics.OverlapBox(spawnPoint, Vector3.one / 4, Quaternion.identity, buildingLayerMask); if (hitCollider.Length > 0) { // Show to player that is not possible because space is occupied return; } else { _newObject = Object.Instantiate(_building, new Vector3(_xPos, 0, _zPos), Quaternion.Euler(new Vector3(_building.transform.localEulerAngles.x, 180, _building.transform.localEulerAngles.z))); _newObject.layer = LayerMask.NameToLayer("Building"); Building building = _newObject.AddComponent <Building>(); if (building) { building.buildingType = _buildingType; BlockProgrammingWindow.BuildBuilding(building.buildingType); } } }