Hi, I think I’ve worked for too long and my brain is fried cause I can’t figure this out.
The logic in my if statement is not working. Particularly the else if at the bottom of the code with lot’s of criteria it executes even if the first if in that statement was completed.
To give you some context: a 2D tilemap world with grass, forests and rivers. This script decides which bridge tile to use: horizontalBridgeTilePrep or verticalBridgeTilePrep - when placing the building, it’s green and it follows the mouse on the tilemap grid till the building is placed down
horizontalBridgeRed - same as before but it’s red signaling that the location is invalid
(The location can be valid if we are trying to place down the bridge on water and if left and right tiles are Grass or Forest then place down horizontal bridge(which means the river flows vertically so the bridge is horizontal). as for vertical bridge it’s the same just rotated…)
The issue: both the horizontal and vertical bridges work great, it’s just that the else if code executes right after the horizontal if statement and overrides the horizontalBridgeTilePrep to horizontalBridgeRed (So it seem like the location is invalid, but it’s not(I’ve made a debug.log that tells prints out all the tiles that are around the place where I’m trying to place the building and it fit’s I’ve also tested it with other buildings works great))
I’ve placed in 2 other debug.logs GREEN and RED and both of them execute(When I’m trying to place down a horizontal bridge). First GREEN and then RED right after that.
if (buildingName == "Bridge" && canPlace == true)
{
if (current == "Water" && (left == "Grass" || left == "Forest") && (right == "Grass" || right == "Forest") && currentL1 == "Nothing")
{
buildingRenderer.SetPrepTileTo(mousePos, horizontalBridgeTilePrep); //Set visuals of the GREEN prep building 1 layer above buildings layer
Debug.Log("GREEN");
if (Input.GetMouseButtonDown(0))
{
buildingRenderer.SetTileTo(mousePos, horizontalBridgeTileConstruction); //Set Visuals
SetTileGridBuildings(mousePos, nameof(horizontalBridgeTileConstruction)); //Set backend grid value
canPlace = temp; //Set it back to false
}
}
if (current == "Water" && (up == "Grass" || up == "Forest") && (down == "Grass" || down == "Forest") && currentL1 == "Nothing")
{
buildingRenderer.SetPrepTileTo(mousePos, verticalBridgeTilePrep); //Set visuals of the GREEN prep building 1 layer above buildings layer
if (Input.GetMouseButtonDown(0))
{
buildingRenderer.SetTileTo(mousePos, verticalBridgeTileConstruction); //Set Visuals
SetTileGridBuildings(mousePos, nameof(verticalBridgeTileConstruction)); //Set backend grid value
canPlace = temp; //Set it back to false
}
}
else if (current != "Water" || currentL1 != "Nothing" || (current == "Water" && (
(up != "Grass" || up != "Forest") && (down != "Grass" || down != "Forest") && (left != "Grass" || left != "Forest") ||
(up != "Grass" || up != "Forest") && (down != "Grass" || down != "Forest") && (right != "Grass" || right != "Forest") ||
(left != "Grass" || left != "Forest") && (right != "Grass" || right != "Forest") && (up != "Grass" || up != "Forest") ||
(left != "Grass" || left != "Forest") && (right != "Grass" || right != "Forest") && (down != "Grass" || down != "Forest")
)))
{
buildingRenderer.SetPrepTileTo(mousePos, horizontalBridgeRed); //Set visuals of the RED prep building 1 layer above buildings layer
Debug.Log("RED");
}
}
else temp = false;