public void InstantiateBuilding() { //If is the last rope... if (name == "RopeBot") { //...Instantiate new building block; var newPredio = Instantiate(buildingGO, transform.position, quaternion.identity); //Rename the build; newPredio.name = "Building Nº" + (numbOfChilds - 3); //Count the child numbers; numbOfChilds += 1; //Check each floor child numbers; rFloor.CheckNumbOfChilds(); gFloor.CheckNumbOfChilds(); bFloor.CheckNumbOfChilds(); } }
//If the building block collides... void OnCollisionEnter2D(Collision2D other) { //...Get the script from the other building; PredioScore otherPredioScore = other.transform.GetComponent <PredioScore>(); //If the script exists... (If collided with another building). if (otherPredioScore != null) { //...If not already collided... if (!collidedWithOtherBuilding && !collidedWithGround) { //...If the other building has the same color... if (otherPredioScore._spriteRenderer.color == _spriteRenderer.color) { //Set this to has the same parent from other building; transform.parent = otherPredioScore.transform.parent; //Fix the scale to not distorce with base in the parent; transform.localScale = new Vector3(1, 1, 1); //Stores the colision position; fixedPos = transform.position; //Stores the number of buildings with the same color that are togheter; int score = transform.parent.parent.GetComponent <FloorScript>().CheckNumbOfChilds(); //Set the score canvas text to the score variable number; _scoreText.text = score.ToString(); //Count the number of childs of each color in the rope script; if (_goToThisFloor == "B") { _ropeMovement.numbOfChildsBlue += 1; } else if (_goToThisFloor == "R") { _ropeMovement.numbOfChildsRed += 1; } else if (_goToThisFloor == "G") { _ropeMovement.numbOfChildsGreen += 1; } } //If the color is different... else { //Destroy the building block in question Destroy(gameObject); } //Set that collided with other building collidedWithOtherBuilding = true; } } //If collided with a floor... FloorScript otherFloor = other.transform.GetComponent <FloorScript>(); if (otherFloor != null) { //...If wasn't collided before... if (!collidedWithGround && !collidedWithOtherBuilding) { //...If number of blue buildings is more than 0 and this build has to go to the blue floor... if (_ropeMovement.numbOfChildsBlue > 0 && _goToThisFloor == "B") { //...Destroy the gameobject; Destroy(gameObject); //Cancel the realocation of score panel; otherFloor.CheckNumbOfChilds(); } //Same for red if (_ropeMovement.numbOfChildsRed > 0 && _goToThisFloor == "R") { Destroy(gameObject); otherFloor.CheckNumbOfChilds(); } //Same for green if (_ropeMovement.numbOfChildsGreen > 0 && _goToThisFloor == "G") { Destroy(gameObject); otherFloor.CheckNumbOfChilds(); } //If the floor that the build most go is the floor that he collided... if (_goToThisFloor == otherFloor.name) { //...Check if the floor have any buildings already if (_goToThisFloor == "B" && _ropeMovement.numbOfChildsBlue <= 0) { //...If this is the first building, add to the count of respective color. _ropeMovement.numbOfChildsBlue += 1; } else if (_goToThisFloor == "R" && _ropeMovement.numbOfChildsRed <= 0) { //...If this is the first building, add to the count of respective color. _ropeMovement.numbOfChildsRed += 1; } else if (_goToThisFloor == "G" && _ropeMovement.numbOfChildsGreen <= 0) { //...If this is the first building, add to the count of respective color. _ropeMovement.numbOfChildsGreen += 1; } //Set the parent to the buildings container; transform.parent = otherFloor.buildingsContainer; //Fix the scale of the object; transform.localScale = new Vector3(1, 1, 1); //Set the score number; int score = other.transform.GetComponent <FloorScript>().CheckNumbOfChilds(); //Set the score text; _scoreText.text = score.ToString(); //Set the scale object to the scale of original object; transform.localScale = buildGO.transform.localScale; //Stores the position of the collision; fixedPos = transform.position; } //If collided in a floor with the wrong collor... else { //...Destroy object and recount the childs; Destroy(gameObject); otherFloor.CheckNumbOfChilds(); } //Set the collision bool to true; collidedWithGround = true; } } }